0

I have this URL:

http://server.com/api.php?results

{"Name":"Pit, Loka","Current":{"Item":"16","test":"test","test":"84","test":"ok"}}

I guess the PHP Example is:

<?php
$file = file_get_contents("http://server.com/api.php?results=item");
$data = json_decode($file);
print_r($data);
?>

but I can't get any results.
How can I solve this problem?

5
  • can u get the result on the browser? Commented May 29, 2015 at 14:50
  • 1
    Check the errors you receive. You either didn't get the contents or json decoding failed. Implement error checks. Commented May 29, 2015 at 14:50
  • Try some debugging: try echo-ing the string in $file, is it empty? Are you seeing any errors ? (error_reporting(E_ALL)) Commented May 29, 2015 at 14:51
  • Did you try var_dump() ? Commented May 29, 2015 at 15:18
  • possible duplicate of Parsing JSON array with PHP foreach Commented May 29, 2015 at 15:26

1 Answer 1

0
<?php
$file = file_get_contents("http://server.com/api.php?results=item");
$data = json_decode($file);

$name = $data->Name; // "Name":"Pit, Loka"
$current = $data->Current->Item; //"Current":{"Item":"16","test":"test","test":"84","test":"ok"}

foreach ($current as $currentX) {
    echo $currentX->Item;
}
?>

It should work... please test.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.