dcblogdev / dcblogcomments

2 stars 0 forks source link

display-xbox-live-data-with-xboxapi-and-cache-it #24

Open utterances-bot opened 4 years ago

utterances-bot commented 4 years ago

Display Xbox live data with xboxapi and cache it - DC Blog

Xbox does not provide an official API for anyone to use. Their are a few sites that provide an AP...

https://dcblog.dev/display-xbox-live-data-with-xboxapi-and-cache-it

chince317 commented 4 years ago

Great tutorial but it looks like the site for the xbox api has changed.

dcblogdev commented 4 years ago

yes, I've just created a free account at https://xapi.us/ the API has changed I'll do an updated tutorial shorty once I've looked at the new API a bit closer.

chince317 commented 4 years ago

Awesome, I can't wait to see the new tutorial and what you come up with.

dcblogdev commented 4 years ago

Until I write the tutorial you can use the following, I've written a class that uses the API:

class XboxApi
{
    protected $apiKey;

    public function __construct($apiKey)
    {
        $this->apiKey = $apiKey;
    }

    public function getData(string $endpoint)
    {
        $filename = str_replace('/', '-', $endpoint);
        $cache_file = "cache/$filename.txt";
        //return cache if less then 5 minutes old
        if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 60 * 5 ))) {
            $content = file_get_contents($cache_file);
            $content = json_decode($content, true);
        } else {
            //cache expired get fresh data
            $content = file_get_contents("https://xapi.us/v2/$endpoint", false, stream_context_create([
                'http'=> [
                  'method'=> "GET",
                  'header'=> "X-AUTH: ".$this->apiKey
                ]
            ]));
            //cache content
            file_put_contents($cache_file, json_encode($content), LOCK_EX);
        }
        return json_decode($content);
    }

    public function pr($object)
    {
        echo "<pre>";
        print_r($object);
        echo "</pre>";
    }
}

To use it create a new instance of the class and pass in your API key (you get the key from your account once logged in at https://xapi.us/profile)

$xbox = new XboxApi('your-api-key-here');

Then you can call an endpoint the url for the API is already set so you would pass /profile to get the profile details:

$item = $xbox->getData('profile');

The $item is an object you can get items out of. To look at the full properties of an object I've provided a pr() method:

$xbox->pr($item);

Here are some samples:

$xbox = new XboxApi('your-api-key-here');

//get account details for the xuid
$account = $xbox->getData('accountXuid');

//gamecard
$item = $xbox->getData("$account->xuid/gamercard");
echo "<h2>Gamecard</h2>";
echo "<p><b>Gamertag</b> $item->gamertag</p>";
echo "<p><b>Name</b> $item->name</p>";
echo "<p><b>Location</b> $item->location</p>";
echo "<p><b>Gamerscore</b> $item->gamerscore</p>";
echo "<p><b>Tier</b> $item->tier</p>";
echo "<p><b>Motto</b> $item->motto</p>";
echo "<p><b>Avatar</b> <img src='$item->avatarBodyImagePath' alt='Avatar'></p>";

echo "<h2>Xbox One Games</h2>";
$items = $xbox->getData("$account->xuid/xboxonegames");
foreach($items->titles as $row) {
    echo "<p><b>Name</b> $row->name</p>";
    echo "<p><b>Earned Achievements</b> $row->earnedAchievements</p>";
    echo "<p><b>Current Gamerscore</b> $row->currentGamerscore/$row->maxGamerscore</p>";
    echo "<p><b>Current Gamerscore</b> ".date('jS F Y H:i A', strtotime($row->lastUnlock))."</p><hr>";
}
chince317 commented 4 years ago

Thanks for the information but I'm having some issues on pulling the information I'm looking for. I'll wait to for the full tutorial to come out as it might help.

Once again thank you for the information and the work.