PHP - JSON Steam API query
Posted
by
Hunter
on Stack Overflow
See other posts from Stack Overflow
or by Hunter
Published on 2013-11-12T23:16:20Z
Indexed on
2013/11/13
15:54 UTC
Read the original article
Hit count: 244
First time using "JSON" and I've just been working away at my dissertation and I'm integrating a few features from the steam API.. now I'm a little bit confused as to how to create arrays.
function test_steamAPI()
{
$api = ('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key='.get_Steam_api().'&steamids=76561197960435530');
$test = decode_url($api);
var_dump($test['response']['players'][0]['personaname']['steamid']);
}
//Function to decode and return the data.
function decode_url($url)
{
$decodeURL = $url;
$data = file_get_contents($url);
$data_output = json_decode($data, true);
return $data_output;
}
So ea I've wrote a simple method to decode Json as I'll be doing a fair bit..
But just wondering the best way to print out arrays..
I can't for the life of me get it to print more than 1 element without it retunring an error e.g. Warning: Illegal string offset 'steamid' in /opt/lampp/htdocs/lan/lan-includes/scripts/class.steam.php on line 48 string(1) "R"
So I can print one element, and if I add another it returns errors.
EDIT -- Thanks for help,
So this was my solution:
function test_steamAPI()
{
$api = ('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key='.get_Steam_api().'&steamids=76561197960435530,76561197960435530');
$data = decode_url($api);
foreach($data ['response']['players'] as $player)
{
echo "Steam id:" . $player['steamid'] . "\n";
echo "Community visibility :" . $player['communityvisibilitystate'] . "\n";
echo "Player profile" . $player['profileurl'] ."\n";
}
}
//Function to decode and return the data.
function decode_url($url)
{
$decodeURL = $url;
$json = file_get_contents($decodeURL);
$data_output = json_decode($json, true);
return $data_output;
}
Worked this out by taking a look at the data.. and a couple json examples, this returns an array based on the Steam API URL (It works for multiple queries.... just FYI) and you can insert loops inside for items etc.. (if anyone searches for this).
© Stack Overflow or respective owner