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
15k4 gold badges43 silver badges59 bronze badges
asked Feb 17, 2012 at 9:21
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.

answered Feb 17, 2012 at 9:24

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'];
answered Feb 17, 2012 at 9:26

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/

answered Feb 17, 2012 at 9:27

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.

Vitalii Fedorenko
115k29 gold badges151 silver badges111 bronze badges
answered Feb 17, 2012 at 9:24

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.