简体   繁体   中英

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. 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". Can anyone see what i am doing wrong? Thanks.

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.

See: https://xboxapi.com/v2/2533274813081462/xboxonegames


Update - Guzzle

I was able to get it working with Guzzle , works with http or 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

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM