0

dear all,
i want to parse this url:
http://core3.oomph.co.id/api_omph_playground/index.php/nav/get_menu/1002/

to get all image, selectedImage, and url value.

and my php syntax is:

<?php
$url1 = "http://core3.oomph.co.id/api_omph_playground/index.php/nav/get_menu/1002/";
$url = file_get_contents("$url1");
$arr = json_decode($url,true);
foreach($arr['menu']['image'] as $item) {
   echo "image: ". $item[0] ."<br>";
   echo "image_selected: ". $item[1] ."<br>";
   echo "url: ". $item[2] ."<br>";
}
?>

but i got Warning: Invalid argument supplied for foreach(), so how to get expected results?

3
Contributors
3
Replies
12
Views
4 Years
Discussion Span
Last Post by pzuurveen
0

Try this...

$url1 = "http://core3.oomph.co.id/api_omph_playground/index.php/nav/get_menu/1002/";
$url = file_get_contents($url1);
$pos = strpos($url,'{');
$json = substr($url,$pos);
$arr = json_decode($json,true);

foreach($arr['menu'] as $item) {
   echo "image: ". $item['image'] ."<br>";
   echo "image_selected: ". $item['selectedImage'] ."<br>";
   echo "url: ". $item['url'] ."<br><br>";
}

There was non-json output at the start of the file - needed to be stripped.

0

thank you, mr.diafol
i did not know it was non-json output.
i use json_decode,because i think it was json output.

thanks anyway.

0

Your file isn't valid json
to create it you have to put all output through json_encode
Also recoment using header("Content-type: text/json"); to create a true json-file

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.