I'm working on a side project and at its core, I need to get a foursquare json feed into an array that i can loop through. My code is below and r开发者_如何学JAVAesults in the following error:
Warning: Invalid argument supplied for foreach() in /homepages/7/d346835943/htdocs/dealrub/results.php on line 56
Here is an example of the json feed that i am correctly acquiring:
$jsonurl = "http://api.foursquare.com/v2/venues/search?ll=".$lat.",".$lon."&limit=100";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_encode($json, true);
foreach ( $json_output->response->groups[0]->items as $items )
{
echo "{$items->name}\n";
}
Any help as to what i'm doing wrong would be greatly appreciated. I left the jsonurl without my api key, but it is successfully returning the json results.
- You have to use
json_decode
. - Check whether
$json_ouput
is not empty. You are passing
true
as second argument tojson_decode
(assuming you have it right) which means that it returns an associative array.Either omit that:
$json_output = json_decode($json);
or access
items
as array:foreach ( $json_output['response']['groups'][0]['items'] as $items )
You're using json_encode on a string that is already in json. Try json_decode instead ;)
精彩评论