我正在尝试学习如何与非官方的xboxapi(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永久移动"。有人看到我做错了什么吗?谢谢
您需要将xuid
替换为实际的xbox配置文件用户id。另外,将InsertAuthCodeHere
替换为API身份验证代码。登录xboxlive后,您可以在您的xboxapi帐户配置文件中找到这两个。
请参阅:https://xboxapi.com/v2/2533274813081462/xboxonegames
更新-枪口
我能够让它与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证书以及使用CURLOPT_FOLLOWLOCATION => true
启用的从http://www.
到https://
的重定向有关
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
$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;