[英]How to interact with XBox API using PHP and cURL
我正在尝试学习如何与非官方的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”。 谁能看到我做错了什么? 谢谢。
您需要将xuid
替换为您的实际xbox配置文件用户ID。 另外,将InsertAuthCodeHere
替换为您的API身份验证代码。 登录到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.
重定向有关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
我得到了答案。 我们缺少必需的花括号。 工作代码:
$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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.