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?

Recommended Answers

All 3 Replies

Member Avatar for diafol

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.

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.