使用 php 向 PlayFab 发送 POST ResetPassword API 请求
send POST ResetPassword API request using php to PlayFab
我正在尝试按照本教程实施 PlayFab 密码恢复过程:
https://docs.microsoft.com/en-us/gaming/playfab/features/engagement/emails/using-email-templates-to-send-an-account-recovery-email
问题是我没有使用 php 的经验,所以我已经努力了好几天才能让它工作但没有成功。我从 PlayFab 数据资源管理器记录的最后一个 activity 是 auth_token_validated 意味着我单击 link 并打开回调 URL。
我确实相信,在一些帮助之后,php 脚本的那部分工作但不是关键功能,API POST。我已尝试利用教程、文档等,但仍然无法使回调正常工作。我确定我还没有弄清楚 php,如果有人能给我一些提示 and/or 帮助,我将不胜感激。
下面是完整的 php 脚本(我的第一个)! ...我知道它可能看起来很糟糕等等,因为我只是想让它工作。同样,我无法 post 返回 PlayFab。我认为问题在@“执行对 PlayFab 的 POST 请求”下面
<!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;
?>
下面是 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>';
我正在尝试按照本教程实施 PlayFab 密码恢复过程: https://docs.microsoft.com/en-us/gaming/playfab/features/engagement/emails/using-email-templates-to-send-an-account-recovery-email
问题是我没有使用 php 的经验,所以我已经努力了好几天才能让它工作但没有成功。我从 PlayFab 数据资源管理器记录的最后一个 activity 是 auth_token_validated 意味着我单击 link 并打开回调 URL。
我确实相信,在一些帮助之后,php 脚本的那部分工作但不是关键功能,API POST。我已尝试利用教程、文档等,但仍然无法使回调正常工作。我确定我还没有弄清楚 php,如果有人能给我一些提示 and/or 帮助,我将不胜感激。
下面是完整的 php 脚本(我的第一个)! ...我知道它可能看起来很糟糕等等,因为我只是想让它工作。同样,我无法 post 返回 PlayFab。我认为问题在@“执行对 PlayFab 的 POST 请求”下面
<!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;
?>
下面是 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>';