I have this code
$feed_Flickr = 'http://api.flickr.com/services/feeds/photos_public.gne?id=44545397@N03&lang=en-us&format=json';
$Flickr = file_get_contents($feed_Flickr);
$Flickr = str_replace('jsonFlickrFeed(','',$Flickr);
$Flickr = str_replace('})','}',$Flickr);
$flickrvalue = json_decode($Flickr);
print_r($flickrvalue);
print_r return nothing what's the wrong with code?
The data isn't valid JSON and that's why json_decode()
isn't working. You can try validating it with a website such as jsonlint.com.
From the json_decode()
documentation:
NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.
That explains why you're not getting any outputs.
UPDATE:
It turns out Flickr escapes single quotes ('
) and apparently this isn't allowed and makes the JSON invalid. You can use str_replace()
to get around this:
$flickrResponse = str_replace("\\'", "'", $Flickr);
Also, as the Flickr API documentation says, instead of using the normal JSON, you can get the raw JSON by appending the nojsoncallback
parameter with the value of 1
to the URL, like so:
http://api.flickr.com/services/feeds/photos_public.gne?id=44545397@N03&lang=en-us&format=json&nojsoncallback=1
So, with that changes, our code should be working:
$feed_Flickr = 'http://api.flickr.com/services/feeds/photos_public.gne?id=44545397@N03&lang=en-us&format=json&nojsoncallback=1';
$Flickr = file_get_contents($feed_Flickr);
$flickrResponse = str_replace("\\'", "'", $Flickr);
$results = json_decode($flickrResponse, true);
print_r($results);