I'm new to PHP and I'm learning some dynamic data retrieval from xml and json files. I have made two codes to do two separate things. The first one is an xml games list data as seen here:
https://steamcommunity.com/id/aksn1p3r/games?tab=all&xml=1
What I would like to do is combine two ways of getting data, from that xml and then, using the variable $appid to get a bit more info from another json data source, before it iterates the next $gdata.
<?php
$glist = 'https://steamcommunity.com/id/aksn1p3r/games?tab=all&xml=1';
$gxml = simplexml_load_file($glist);
foreach($gxml->games->game as $game) {
$appid = $game->appID;
$glink = $game->storeLink;
$gname = $game->name;
$glogo = $game->logo;
echo '<p>'. $appid . ' <a href="'. $glink .'">'. $gname .'<br>';
echo '<img src="'. $glogo .'" /></a></p>';
}
?>
Here is a json: [http://store.steampowered.com/api/appdetails/?appids=8870] that is constructed by using the variable named $appid.
I want it to contruct a new link before the next iteration in the loop above, and then get the price aka $gprice from the json source, as below:
<?php
$appid = '8870';
$ht = 'http://store.steampowered.com/api/appdetails/?appids=' . $appid;
$fgc = file_get_contents($ht);
$jd = json_decode($fgc, true);
$gdata = $jd[$appid]['data'];
$gprice = $gdata['price_overview']['final'];
$price = number_format($gprice / 100, 2);
echo 'Price: $' .$price;
?>
This is the way I tried to combine it but it hangs or errors.
<?php
$glist = 'https://steamcommunity.com/id/aksn1p3r/games?tab=all&xml=1';
$gxml = simplexml_load_file($glist);
foreach($gxml->games->game as $game) {
$appid = $game->appID;
$glink = $game->storeLink;
$gname = $game->name;
$glogo = $game->logo;
echo '<p>'. $appid . ': <a href="'. $glink .'">'. $gname .'<br>';
echo '<img src="'. $glogo .'" /></a><br>';
$gjson = 'http://store.steampowered.com/api/appdetails/?appids=' . $appid;
$fgc = file_get_contents($gjson);
$jd = json_decode($fgc, true);
$gdata = $jd[$appid]['data'];
$gprice = $gdata['price_overview']['final'];
$price = number_format($gprice / 100, 2);
echo 'Price: $' .$price. '</p>';
}
?>
I was expecting it to use the $appid each loop, to construct the link to the source: $gjson, and then file_get_contents($gjson); to fetch the data and then echo that $gprice before moving on to next item.
Any pointers or errors I made, please help me to make it work?
Actually there is nothing wrong in your code itself... the only error is that $appid
in that line should be integer
, not string
.
$gdata = $jd[$appid]['data'];
You think that script hangs - well, it doesn't. It is running fine and doing its work. The problem is the way you are retrieving data - It isn't so fast.
Your list contains over 100 games. So basically your script is opening appdetails remote file and json_decode
s it over 100 times. It takes a while and there is no output until foreach
loop stops executing, so you can think that script hangs.
I modified your code a bit so you can see what is it about.
<?php
$glist = 'https://steamcommunity.com/id/aksn1p3r/games?tab=all&xml=1';
$gxml = simplexml_load_file($glist);
$i = 0; //just used to exit loop after 5 executions
foreach($gxml->games->game as $game) {
if($i >= 5) {
break;
}
$appid = $game->appID;
$glink = $game->storeLink;
$gname = $game->name;
$glogo = $game->logo;
echo '<p>'. $appid . ': <a href="'. $glink .'">'. $gname .'<br>';
echo '<img src="'. $glogo .'" /></a><br>';
$gjson = 'http://store.steampowered.com/api/appdetails/?appids=' . $appid;
$fgc = file_get_contents($gjson);
$jd = json_decode($fgc, true);
$gdata = $jd[intval($appid)]['data']; //$appid needs to be integer here, not string
$gprice = $gdata['price_overview']['final'];
$price = number_format($gprice / 100, 2);
echo 'Price: $' .$price. '</p>';
$i++;
}
?>
Run it and wait few seconds. It should display 5 games with their prices (or error if game is free to play...).
Now, if it takes about five seconds to load images and prices of five games, It will take about 2 minutes to process all 100+ games. You should do it other way if you don't want to wait so long.