1

i have this public JSON https://raw.github.com/currencybot/open-exchange-rates/master/latest.json and i need to extract data from it, right now i tried something like this:

$input = file_get_contents("https://raw.github.com/currencybot/open-exchange-rates/master/latest.json");

$json = json_decode($input);

echo $json[rates]->EUR;

but i obtain a blank page, any suggestion? Thanks !! Ste

Wesley van Opdorp
  • 14,888
  • 4
  • 41
  • 59

4 Answers4

6

json_decode either returns an object, or an array (when you use the second param). You are trying to use both.

Try:

$json = json_decode($input);    
echo $json->rates->EUR;

OR

$json = json_decode($input, true);
echo $json['rates']['EUR'];

As to the blank page, please add the following to the top of your script:

error_reporting(E_ALL);
init_set('display_errors', true);

A 500 Error indicates you are unable to resolve that url using file_get_contents.

Check here for more information.

Community
  • 1
  • 1
Wesley van Opdorp
  • 14,888
  • 4
  • 41
  • 59
0

The link looks like starting with https, while file_get_contents can't deal with SSL. My suggestion is using curl.

Vitalii Fedorenko
  • 110,878
  • 29
  • 149
  • 111
  • 1
    file_get_contents CAN deal with those links if PHP was built with ssl, or if you've written and registered your own https stream – Mark Baker Feb 17 '12 at 09:28
0

Read the help for json_decode ... without the second parameter it returns and object not an array ...

Either :

$json = json_decode($input);
echo $json->rates->EUR;

or

$json = json_decode($input,true);
echo $json['rates']['EUR'];
Manse
  • 37,765
  • 10
  • 83
  • 108
0

Try:

$arr = json_decode($input, true);

var_dump($arr);
echo $arr['rates']['EUR'];

Further Reading: http://de.php.net/manual/de/function.json-encode.php

For anexample with cURL and SSL read this: http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/

Julian Hollmann
  • 2,902
  • 2
  • 25
  • 44