且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用PHP和cURL与XBox API交互

更新时间:2023-02-08 10:26:00

您需要将 xuid 替换为实际的xbox配置文件用户名。
另外用您的API验证码替换 InsertAuthCodeHere
登录Xbox Live后,您可以在您的xboxapi帐户配置文件中找到。

You need to replace xuid with your actual xbox profile user id. Additionally replace InsertAuthCodeHere with your API auth code. You can find both on your xboxapi account profile after logging into xbox live.

请参阅: 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 =>验证SSL证书有关。 false 以及从 http:// www。 https:// 其中启用 CURLOPT_FOLLOWLOCATION => true

The issue is related to validating the SSL certificate via CURLOPT_SSL_VERIFYPEER => false and the redirect from http://www. to https:// occurring which is enabled with 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