[英]How to interact with XBox API using PHP and cURL
I am trying to learn how to interact with the unofficial xbox api (xboxapi.com) but I can't seem to figure out how to use it. 我正在尝试学习如何与非官方的xbox api(xboxapi.com)进行交互,但我似乎无法弄清楚如何使用它。 The documentation is very scarce.
文档非常稀缺。 This is my most recent (and what i thought best) attempt.
这是我最近的(也是我认为最好的)尝试。
<?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;
?>
Upon running the above I get "301 Moved Permanently". 运行上面的命令后,我得到“301 Moved Permanently”。 Can anyone see what i am doing wrong?
谁能看到我做错了什么? Thanks.
谢谢。
You need to replace xuid
with your actual xbox profile user id. 您需要将
xuid
替换为您的实际xbox配置文件用户ID。 Additionally replace InsertAuthCodeHere
with your API auth code. 另外,将
InsertAuthCodeHere
替换为您的API身份验证代码。 You can find both on your xboxapi account profile after logging into xbox live. 登录到xbox live后,您可以在xboxapi帐户配置文件中找到它们。
See: https://xboxapi.com/v2/2533274813081462/xboxonegames 请参阅: https : //xboxapi.com/v2/2533274813081462/xboxonegames
Update - Guzzle 更新 - Guzzle
I was able to get it working with Guzzle , works with http
or https
我能够使用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
Update 2 - cURL 更新2 - cURL
The issue is related to validating the SSL certificate via CURLOPT_SSL_VERIFYPEER => false
and the redirect from http://www.
该问题与通过
CURLOPT_SSL_VERIFYPEER => false
验证SSL证书以及从http://www.
重定向有关http://www.
to https://
occurring which is enabled with CURLOPT_FOLLOWLOCATION => true
到
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
I got an answer. 我得到了答案。 We were missing required curly braces.
我们缺少必需的花括号。 Working code:
工作代码:
$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.