Demos, Tutorials, PHP & MySQL

Xbox does not provide an official API for anyone to use. Their are a few sites that provide an API one of which is https://xboxapi.com/v1 xboxapi makes it very easy to pull your Xbox API data.

In this tutorial I'll show you how to display your Xbox Live data and then cache it, this will enable the page to load faster and avoid going over xboxapi's API Limit which is set to 350 requests an hour.

Cache

In order to cache the data we need to build a cache system, but I've chosen to use a prebuilt one from https://github.com/gilbitron/PHP-SimpleCache

The SimpleCache class is very easy to use, download simpleCache.php and save that in your project root, I've called the file cache.php

Next create a cache folder, this folder will hold all the cache data.

Finally create a php file called index.php.

Open index.php, then include cache.php create a new instance of the SimpleCache class and pass in 2 parameters the first is the label for the cache. The second is the url to be cached.

the url to be called is https://xboxapi.com/v1/json/profile/ followed by your gamertag this will return a json object containing your Xbox Live API Data. Once called the cache will download the json object and store it as a text file inside the cache folder, next time the url is requested the cache version will be returned if the call is within the cache time, I've set the cache time to 600 ms which is 10 minutes. The cache time can be changed by editing cache.php.

Once the json object is returned pass it through a json_decode call to be able to use the data.

require('cache.php'); 
$cache = new SimpleCache();
$json = $cache->get_data('gamester247', 'https://xboxapi.com/v1/json/profile/gamester247');
$obj = json_decode($json);

The raw json object will return like this:

{
"Success": true,
"API_Limit": "0/350",
"Player": {
"Gamertag": "gamester247",
"Status": {
"Valid": true,
"Tier": "Gold",
"Online": false,
"Online_Status": "Last seen 16 hours ago playing Dead Spaceâ„¢ 3",
"Cheater": false
},
"Launch_Team": {
"X360": false,
"NXE": false,
"Kinect": false
},
"Avatar": {
"Gamertile": {
"Small": "https://avatar-ssl.xboxlive.com/global/t.fffe07d1/tile/0/11016",
"Large": "https://avatar-ssl.xboxlive.com/global/t.fffe07d1/tile/0/21016"
},
"Gamerpic": {
"Small": "https://avatar-ssl.xboxlive.com/avatar/gamester247/avatarpic-s.png",
"Large": "https://avatar-ssl.xboxlive.com/avatar/gamester247/avatarpic-l.png"
},
"Body": "https://avatar-ssl.xboxlive.com/avatar/gamester247/avatar-body.png"
},
"Gamerscore": 25905,
"Reputation": 300,
"Name": "David Carr",
"Motto": null,
"Location": "Hull, England",
"Bio": "raptr-fee8c3d8"
},
"RecentGames": [
{
"ID": 1161890205,
"Name": "Dead Spaceâ„¢ 3",
"MarketplaceURL": "http://marketplace.xbox.com/en-US/Title/1161890205",
"BoxArt": {
"Small": "http://catalog.xboxapi.com/image/66acd000-77fe-1000-9115-d8024541099d/boxartsm.jpg",
"Large": "http://catalog.xboxapi.com/image/66acd000-77fe-1000-9115-d8024541099d/boxartlg.jpg"
},
"CatalogLink": "http://catalog.xboxapi.com/1161890205"
},
{
"ID": 1096157351,
"Name": "[PROTOTYPE®2]",
"MarketplaceURL": "http://marketplace.xbox.com/en-US/Title/1096157351",
"BoxArt": {
"Small": "http://catalog.xboxapi.com/image/66acd000-77fe-1000-9115-d802415608a7/boxartsm.jpg",
"Large": "http://catalog.xboxapi.com/image/66acd000-77fe-1000-9115-d802415608a7/boxartlg.jpg"
},
"CatalogLink": "http://catalog.xboxapi.com/1096157351"
},
{
"ID": 1414793383,
"Name": "Grand Theft Auto V",
"MarketplaceURL": "http://marketplace.xbox.com/en-US/Title/1414793383",
"BoxArt": {
"Small": "http://catalog.xboxapi.com/image/66acd000-77fe-1000-9115-d802545408a7/boxartsm.jpg",
"Large": "http://catalog.xboxapi.com/image/66acd000-77fe-1000-9115-d802545408a7/boxartlg.jpg"
},
"CatalogLink": "http://catalog.xboxapi.com/1414793383"
},
{
"ID": 1096157269,
"Name": "Call of Duty Black Ops",
"MarketplaceURL": "http://marketplace.xbox.com/en-US/Title/1096157269",
"BoxArt": {
"Small": "http://catalog.xboxapi.com/image/66acd000-77fe-1000-9115-d80241560855/boxartsm.jpg",
"Large": "http://catalog.xboxapi.com/image/66acd000-77fe-1000-9115-d80241560855/boxartlg.jpg"
},
"CatalogLink": "http://catalog.xboxapi.com/1096157269"
},
{
"ID": 1161890256,
"Name": "Titanfall",
"MarketplaceURL": "http://marketplace.xbox.com/en-US/Title/1161890256",
"BoxArt": {
"Small": "http://catalog.xboxapi.com/image/66acd000-77fe-1000-9115-d802454109d0/boxartsm.jpg",
"Large": "http://catalog.xboxapi.com/image/66acd000-77fe-1000-9115-d802454109d0/boxartlg.jpg"
},
"CatalogLink": "http://catalog.xboxapi.com/1161890256"
}
],
"API_Links": {
"Friends": "https://xboxapi.com/v1/json/friends/gamester247",
"Games": "https://xboxapi.com/v1/json/games/gamester247",
"JSON": {
"Friends": "https://xboxapi.com/v1/json/friends/gamester247",
"Games": "https://xboxapi.com/v1/json/games/gamester247"
},
"XML": {
"Friends": "https://xboxapi.com/v1/xml/friends/gamester247",
"Games": "https://xboxapi.com/v1/xml/games/gamester247"
},
"PHP": {
"Friends": "https://xboxapi.com/v1/php/friends/gamester247",
"Games": "https://xboxapi.com/v1/php/games/gamester247"
},
"Development": {
"Friends": "https://xboxapi.com/v1/dev/friends/gamester247",
"Games": "https://xboxapi.com/v1/dev/games/gamester247"
}
}
}

Looking through the data xboxapi provide other API calls for Friends and Games, I won't be using them in this article.

To use the data first check their is data to use by checking if their is a success property equal to 1

if($obj->Success == 1){

To display the data, call the object then specify the property you want followed by any subsequent properties:

echo $obj->Player->Gamertag;

A more complete examples:

<?php
    //if success
    if($obj->Success == 1){
        echo 'API Hourly Limit: '.$obj->API_Limit.'<br>';
        echo 'Gamertag: '.$obj->Player->Gamertag.'<br>';
        echo 'Membership: '.$obj->Player->Status->Tier.'<br>';
        
        //checl if user is online
        if($obj->Player->Status->Online == 1){
            echo 'Online<br>';
        } else {
            echo 'Offline<br>';
        }

        echo $obj->Player->Status->Online_Status.'<br>';
        echo "<img src='".$obj->Player->Avatar->Gamertile->Large."' alt=''><br>";
        echo 'Gamerscore: '.$obj->Player->Gamerscore.'<br>';
        echo 'Reputation: '.$obj->Player->Reputation.'<br>';
        echo $obj->Player->Name.'<br>';
        echo $obj->Player->Location.'<br>';

        echo "<p>";
        //loop through all recent games and display image
        foreach($obj->RecentGames as $row){
            echo "<img src='".$row->BoxArt->Small."' alt='$row->Name' title='$row->Name'> ";    
        }
        echo "</p>";
    } else {
        //display the error from the API
        echo $obj->Error;
    }
    ?>