首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用php向ResetPassword发送POST PlayFab API请求

使用php向ResetPassword发送POST PlayFab API请求
EN

Stack Overflow用户
提问于 2022-03-03 00:22:35
回答 1查看 125关注 0票数 0

在本教程之后,我尝试实现PlayFab密码恢复过程:https://learn.microsoft.com/en-us/gaming/playfab/features/engagement/emails/using-email-templates-to-send-an-account-recovery-email

实际请求:https://learn.microsoft.com/en-us/rest/api/playfab/admin/account-management/reset-password?view=playfab-rest

问题是,我没有使用php的经验,所以我花了几天的时间才使它成功,但没有成功。我从PlayFab数据资源管理器记录的最后一个活动是auth_token_validated,这意味着我单击该链接并打开回调URL。

我相信,经过一些帮助,php脚本的一部分可以工作,但关键函数API POST不起作用。我尝试过利用教程、文档等,但仍然无法使回调工作。我相信我还没有计算出php,如果有人能给我一些提示和/或帮助,我会非常感激的。

下面是完整的php脚本(我的第一个)!...I知道它看起来可能很糟糕等等,因为我只是想让它正常工作。同样,我无法回发到PlayFab。我认为下面这个问题是@“向PlayFab执行POST请求”

代码语言:javascript
复制
<!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;
?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-13 08:31:25

下面是PlayFab的实际代码片段的一个工作示例:

代码语言:javascript
复制
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>';
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71330740

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档