Skip to content
Advertisement

send POST ResetPassword API request using php to PlayFab

I am trying to implement PlayFab password recovery process following this tutorial: https://docs.microsoft.com/en-us/gaming/playfab/features/engagement/emails/using-email-templates-to-send-an-account-recovery-email

The actual request: https://docs.microsoft.com/en-us/rest/api/playfab/admin/account-management/reset-password?view=playfab-rest

The problem is that I have no experience with php so I have been struggling for days to get this to work and not succeeded. The last activity I record from PlayFab Data Explorer is auth_token_validated meaning I click on the link and open the callback URL.

I do believe, after some help, that part of the php script works but not the key function, the API POST. I have tried to leverage tutorials, documentations etc. but can still not get the callback to work. I am sure I have not figured out php and would really appreciate if someone could give me a few hints and/or help.

Below is the full php script (my first)! …I know it probably looks terrible etc. as I just trying to get it to work. Again, I am not able to post back to PlayFab. The problem I believe is below @ “Perform the POST request to PlayFab”

<!DOCTYPE HTML>  
<html>
<head>
<style>
.error {color: #FF0000;}
.button {
    height: 80px;
    width: 130px;
}
</style>
</head>
<body>  

<?php
    // define variables and set to empty values
    $emailErr = $pw1Err = $pw2Err = $check = "";
    $email = $pw1 = $pw2 = $check = $params = $link = "";

    // Here append the common URL characters.
    $link .= "://";
  
    // Append the host(domain name, ip) to the URL.
    $link .= $_SERVER['HTTP_HOST'];
  
    // Append the requested resource location to the URL
    $link .= $_SERVER['REQUEST_URI'];


    // Initialize URL to the variable
    //$url = 'https://www.geeksforgeeks.org?name=Tonny';
    $url = $link;    //'https://www.example.com/?token=2346241B7C277796';
  
// Use parse_url() function to parse the URL 
// and return an associative array which
// contains its various components
$url_components = parse_url($url);

// Use parse_str() function to parse the
// string passed via URL
parse_str($url_components['query'], $params);

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    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";
        }
    }



    if (empty($_POST["pw1"])) 
    {
        $pw1Err = "Password is required";
    }
    else 
    {
        $pw1 = test_input($_POST["pw1"]);
    }



    if (empty($_POST["pw2"])) {
        $pw2Err = "Confirm Password is required";
    }
    else 
    {
        $pw2 = test_input($_POST["pw2"]);


        if (strcasecmp($pw1, $pw2) == 0) {
            $check = "Password is OK";
        } 
        else 
        {
            $check = "Password is NOT OK";
        }


        // Perform the POST request to PlayFab
        $url = "https://titleId.playfabapi.com/Admin/ResetPassword";

        $data = array('Password' => $pw1, 'Token' => $params['token']);

        $options = array(
            'http' => array(
                'header'  => "X-SecretKey: xxxxxxxxxxxxx",
                'method'  => 'POST',
                'content' => http_build_query($data)
            )
        );

        $context  = stream_context_create($options);
        $resp = file_get_contents($url, false, $context);
        var_dump($resp);

        // ob_start();
        // var_dump($resp);
        // $result = ob_get_clean();
        

        // ======
    }

    // Program to display URL of current page.
    if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') {
        $link = "https";
    }
    else 
    {
        $link = "http";
    }
  
    
}

function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
?>

<h2>Password Recovery</h2>
<p><span class="error">* required field</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
    E-mail: <input type="text" name="email" value="<?php echo $email;?>">
    <span class="error">* <?php echo $emailErr;?></span>
    <br><br>
    New Password: <input type="test_input" name=pw1 valur="<?php echo $pw1;?>">
    <span class="error">* <?php echo $pw1Err;?></span>
    <br><br>
    Confirm Password: <input type="test_input" name=pw2 valur="<?php echo $pw2;?>">
    <span class="error">* <?php echo $pw2Err;?></span>
    <br><br><br>
    <input type="submit" class="button" name="submit" value="Submit" if="myButton" /> 
</form>

<?php
    echo "<br>";
    echo "<h3>Your Input:</h3>";
    echo $email;
    echo "<br>";
    echo $pw1Err;
    echo "<br>";
    echo $pw2Err;
    echo "<br>";
    echo $check;
    echo "<br>";
    echo ' The Token: '.$params['token'];
    echo "<br>";
    echo ' Link: '.$link;
    echo "<br>";
    echo ' resp: '.$result;
?>

Advertisement

Answer

So here is a working example of the actual code snippet to PlayFab:

echo '=============== START ===============';
    echo '<br>';


    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, 'https://99999.playfabapi.com/Admin/ResetPassword?Password=lisalisa&Token=43D816F247CD3E10');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);

    $headers = array();
    $headers[] = 'X-Secretkey: xxxxxx';
    $headers[] = 'Content-Type: application/json';
    
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $result = curl_exec($ch);

    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
        echo '<br>';
    }
    else
    {
        echo '>>> WORKS <<<<';
        echo '<br>';
    }

    curl_close($ch);

    echo '>> ' . $result;
    echo '<br>';

    echo '=============== END ===============';
    echo '<br>';
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement