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

1
  • 3
    Did you already try $json->rates->EUR? Commented Feb 17, 2012 at 9:23

4 Answers 4

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.

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

2 Comments

hi guys, thanks for answers, but remains the same, blank page :/
Please see my update, could solve your white screen of death.
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'];

Comments

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/

4 Comments

it gives "NULL", don't know why :)
Then you get no result from file_get_contents(). Try cURL instead
can you give me an example with cURL? i'm looking for info but im a little bit confused, thanks:)
This method is explained here: unitstep.net/blog/2009/05/05/…
0

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

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.