我正在尝试学习如何与非官方的xbox api(xboxapi.com)进行交互,但我似乎无法弄清楚如何使用它。文档非常稀缺。这是我最近的(也是我认为最好的)尝试。
<?php
$gamertag = rawurlencode("Major Nelson");
$ch = curl_init("http://www.xboxapi.com/v2/xuid/" . $gamertag);
$headers = array('X-Auth: InsertAuthCodeHere', 'Content-Type: application/json');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); # return into a variable
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers ); # custom headers, see above
$xuid = curl_exec( $ch ); # run!
curl_close($ch);
echo $xuid;
?>
运行上述内容后,我获得“301 Moved Permanently”。谁能看到我做错了什么?感谢。
答案 0 :(得分:2)
您需要将xuid
替换为您的实际xbox个人资料用户ID。
另外,使用您的API身份验证代码替换InsertAuthCodeHere
。
登录到xbox live后,您可以在xboxapi帐户配置文件中找到它们。
请参阅:https://xboxapi.com/v2/2533274813081462/xboxonegames
更新 - Guzzle
我能够使用Guzzle,与http
或https
一起使用
require __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/config.php'; //defines XboxAPI_Key
$gamertag = isset($_GET['gamertag']) ? urldecode($_GET['gamertag']) : 'Major Nelson';
$url = 'http://xboxapi.com/v2/xuid/' . rawurlencode($gamertag);
$guzzle = new GuzzleHttp\Client();
$response = $guzzle->get($url, [
'headers' => [
'X-Auth' => XboxAPI_Key,
'Content-Type' => 'application/json'
],
]);
echo $response->getBody(); //2584878536129841
更新2 - cURL
此问题与通过CURLOPT_SSL_VERIFYPEER => false
验证SSL证书以及http://www.
到https://
的重定向有关,该版权已由CURLOPT_FOLLOWLOCATION => true
启用
require_once __DIR__ . '/config.php';
$gamertag = isset($_GET['gamertag']) ? urldecode($_GET['gamertag']) : 'Major Nelson';
$url = 'http://www.xboxapi.com/v2/xuid/' . rawurlencode($gamertag);
/**
* proper url for no redirects
* $url = 'https://xboxapi.com/v2/xuid/' . rawurlencode($gamertag);
*/
$options = [
CURLOPT_RETURNTRANSFER => true, // return variable
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_AUTOREFERER => true, // set referrer on redirect
CURLOPT_SSL_VERIFYPEER => false, //do not verify SSL cert
CURLOPT_HTTPHEADER => [
'X-Auth: ' . XboxAPI_Key
]
];
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
echo $content; //2584878536129841
答案 1 :(得分:0)
我得到了答案。我们缺少必需的花括号。工作代码:
$gamertag = rawurlencode("Major Nelson");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://xboxapi.com/v2/xuid/{$gamertag}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"X-Auth: InsertAuthCode",
]);
$output = curl_exec($ch);
curl_close ($ch);
print $output;