Z
Zip99

  • Jul 27, 2022
  • Joined Mar 7, 2022
  • NogDog First I really want to thank you for your response, really appreciated. I do not know why I have the back-slashes but probably from one of the numerous examples I have tested. As I explained that was my first php script ever so it is not polished, which I now will fix.

    However, I did test Postman and was able to finally get this to work using cURL. here is the code snippet for the actual API I use. BTW the back-slash is from Postman code generator.

    `echo '>>>>>>>>>> TEST <<<<<<<<<<';

        $curl = curl_init();
    
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://999999.playfabapi.com/Admin/ResetPassword?Password=pekapeka&Token=1B5A9C01EFD5DB69',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_HTTPHEADER => array(
                'X-SecretKey: xxxxxxx',
                'Content-Type: application/json\\'
            ),
        ));
    
        $response = curl_exec($curl);
    
        curl_close($curl);
        echo $response;
        print_r($response);
    
        echo '<br>========== END ==========';`
    • I am new to php and need to create a PlayFab ResetPassword API: https://docs.microsoft.com/en-us/rest/api/playfab/admin/account-management/reset-password?view=playfab-rest

      I have tried to learn php the last week and I believe I have been a bit successful but have an error I do not know how to solve. I get the following error:

      {"code":400,"status":"BadRequest","error":"InvalidContentType","errorCode":1144,"errorMessage":"Request HTTP header: \"Content-Type: application/x-www-form-urlencoded\" does not match required value: application/json"}

      Here is the API portion of the code:

      `$data = array('Password' => $pw1, 'Token' => $params['token']);
      $hData = array('X-SecretKey' => 'xxxxxxxxxxxxx', 'Content-Type' => 'application/json\');

                          $options = array(
                              'http' => array(
                                  'method'  => 'POST',
                                  'content' => http_build_query($data),
                                  'header' => http_build_query($hData),
                                  'ignore_errors' => true
                              )
                          );
      
                          $context  = stream_context_create($options);
                          $resp = file_get_contents($url, false, $context);
                          var_dump($resp);`

      I have no idea how to solve this and have really tried using different resources and need help!

      Here is the full code, and yes it is awfully bad but it is only to try to get the API to hit:

      <!DOCTYPE html>
      <head>
      <meta charset="utf-8">
      <title>Password Recovery</title>
      <meta name="viewport" content="width=device-width">
      <link rel="stylesheet" href="style.css" type="text/text/css" media="screen" charset="utf-8">
      <style>
      .error {color #FF0000;}
      .button {
      background-color: black;
      border: none;
      color: white;
      padding: 15px 32px;
      text-align: center;
      text-decoration: none;
      display: inline-block;
      margin: 4px 2px;
      cursor: pointer;
      font-size: 15px;
      }
      html, body {

              }
          </style>
      </head>
      
      <?php
          ini_set('display_errors', 1);
          //error_reporting(E_ALL);
          //error_reporting(E_NOTICE);
          header("Content-Type: text/html");
      
      
      
          // application/x-www-form-urlencoded\
      
      
          $emailErr = "";
          $email = "";
          $url_components = "";
          $token = "";
          $myToken = "";
          $pw1 = "";
          echo $POST['pw1'];
          $pw1Err = "";
          $pw2 = "";
          echo $POST['pw2'];
          $pw2Err = "";
          $message = "";
          $apiUrl = "";
          $apiData = "";
          $titleID = "67833";
          echo $POST['titleID'];
      
          // Test validity of the password
          function test_input($data) {
              $data = trim($data);
              $data = stripslashes($data);
              $data = htmlspecialchars($data);
              return $data;
          }
      
      
          // 1) Get the link
          $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
          //$actual_link = "http://www.linkopinghistoria.se/TEST_PK/?token=2652765099E8BD3D&lang=en";
      
          // 2) Extract token from url
          $url_components = parse_url($actual_link);
          parse_str($url_components['query'], $token);
      
          $myToken = $token['token'];
          echo $POST['myToken'];
      
      
      
          if ($_SERVER["REQUEST_METHOD"] == "POST") {
      
              if (isset($_POST['submit']))
              {
                  if (empty($_POST["email"]))
                  {
                      $emailErr = "Email is required";
                  }
                  else
                  {
                      $email = test_input($_POST["email"]);
      
                      // check if e-mail address is well-formed
                      if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                          $emailErr = "Invalid email format";
                      }
                  }
                  echo $_POST['message'];
                  echo '<br><br>';
      
                  if (empty($_POST["pw1"]))
                  {
                      $pw1Err = "Required";
                  }
                  else
                  {
                      if (strlen($pw1) >=6) {
                          //$pw1 = test_input($_POST["pw1"]);
                      }
                      else {
                          if (strlen($pw1) < 6) {
                              $pw1Err = "Password to short (<6 chars)";
                          }
                      }
                  }
      
                  if (empty($_POST["pw2"])) {
                      $pw2Err = "Required";
                  }
                  else
                  {
                      //3) Get the password
                      $pw1 = $_POST['pw1'];
                      $pw2 = $_POST['pw2'];
      
                      if (strcasecmp($pw1, $pw2) == 0) {
                          echo "Password is OK";
                          echo "<br>";
                          echo "pw1: " . $pw1;
                          echo "<br>";
                          echo "myToken: " . $myToken;
                          echo "<br>";
      
                          // =========================================
                          // Perform the POST request to PlayFab
                          $url = "https://67833.playfabapi.com/Admin/ResetPassword";
                          echo 'url: ' . $url;
                          echo '<br>';
                          echo 'finalUrl: ' . $finalUrl;
                          echo '<br>';
      
                          $data = array('Password' => $pw1, 'Token' => $params['token']);
                          $hData = array('X-SecretKey' => 'xxxxxxxxxx', 'Content-Type' => 'application/json\\');
      
                          $options = array(
                              'http' => array(
                                  'method'  => 'POST',
                                  'content' => http_build_query($data),
                                  'header' => http_build_query($hData),
                                  'ignore_errors' => true
                              )
                          );
      
                          $context  = stream_context_create($options);
                          $resp = file_get_contents($url, false, $context);
                          var_dump($resp);
      
                          echo "<br>";
                          echo 'option: ';
                          print_r($option);
                          echo "<br>";
                          echo 'context: ';
                          print_r($context);
                          echo "<br>";
                          echo 'var_dump: ';
                          echo '<pre>' , var_dump($resp) , '</pre>';
                          echo "<br>";
                          print_r($resp);
                          echo "<br>";
      
      
                          // ob_start();
                          // var_dump($resp);
                          // $result = ob_get_clean();
      
      
      
                          // =========================================
                      }
                      else
                      {
                          $pw2Err = "Password is not similar";
                      }
                  }
              }
          }
      
      
      ?>
      
      <body>
          <div if="content">
              <form action="" method="post" accept-charset="utf-8">
                  <p><span class="error">*required field</span></p>
                  <h1>Password Recovery</h1>
                  <br><br>
                  E-mail: <input type="email" name="email" value="<?php echo $email;?>">
                      <span class="error">*<?php echo $emailErr;?></span>
                      <?php echo $POST['email'];?>
                  <br><br>
      
                  New Password : <input type="password" name="pw1" value="<?php echo $pw1;?>">
                      <span class="error">* <?php echo $pw1Err;?></span>
                      <?php echo $POST['pw1'];?>
      
                  <br>
      
                  Confirm Password : <input type="password" name="pw2" value="<?php echo $pw2;?>">
                      <span class="error">* <?php echo $pw2Err;?></span>
                      <?php echo $POST['pw2'];?>
      
                  <br><br>
      
                  <input type="submit" class="button" name="submit" value="Submit"/>
      
      
              </form>
          </div>
          <?php
              echo 'EMAIL: ' . $_POST['email'];
              echo '<br>';
              echo 'LINK: ' . $actual_link;
              echo '<br>';
              echo 'TOKEN: ' . $myToken;
              echo '<br>';
              echo 'PW1: ' . $_POST['pw1'];
              echo '<br>';
              echo 'PW2: ' . $_POST['pw2'];
              echo '<br>';
              echo 'MESSAGE: ' . $_POST[message];
          ?>
      </body>

      </html>