Search code examples
phpjsondecodexbox

Decoding JSON into an array for a xbox live image script?


I'm trying to turn a JSON file of Xbox Live data into variables that I can use in a PHP generated image. My JSON file is here: http://www.xboxgamercard.org/gamercard/test3/xbox.php

I have tried this:

$request_url = 'xbox.php';
$json = file_get_contents($request_url);
$decode = json_decode($json, true);
var_dump($decode['gamertag'][0]);

but it just returns NULL.

I would like to use the JSON as shown here:

$gamertag = $data['Gamertag'];
echo $gamertag;

Solution

  • You need to add the full url eg:

    <?php
    $request_url = 'http://www.xboxgamercard.org/gamercard/test3/xbox.php';
    
    $json = file_get_contents($request_url);
    $data = json_decode($json, true);
    
    //Example output
    echo $data['gamertag']; //Crylics
    echo $data['gamerscore']; //7492
    
    /* Too access the recent_games key you will need
       to loop through it or access it like
    */
    echo $data['recent_games'][1]['title']; //Call of Duty: WaW
    ?>