I’m new to json decoding with php. here’s my situation. I need to grab a very simple json call and parse it on my HTML page. Here’s the contents of that json:
[{"id":"793","date":"Apr. 18, 2011","title":"the cat in the hat","url":"http:\/\/www.somesite.com\/community\/blogs\/id-793"},{"id":"788","date":"Apr. 12, 2011","title":"the fox has sox","url":"http:\/\/www.somesite.com\/community\/blogs\/id-788"}]
etc…
When I go ahead and put that in a string in my php file:
$json = [{...}]
$arr = json_decode($json,true);
foreach($arr as $item) {
echo "title: ". $item['title'] ."<br>";
}
I get the titles printed out w/o a problem. If, instead, I put:
$arr = json_decode("url_path_here",true);
foreach($arr as $item) {
echo "title: ". $item['title'] ."<br>";
}
I get an error, “Invalid argument supplied for foreach() ” and the array is simply empty.
The feed is on a different server during development, if that matters. I’m just not sure what I’m doing wrong here. I’m using PHP 5.
Thanks!
json_decodetakes a string, not a URL.This will likely work: