0

I am trying to get data from JSON array.

<?php


$query = "https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=USD&to_currency=JPY&apikey=demo";


$forexdecode = json_decode(file_get_contents($query));

echo '<pre>';
print_r($forexdecode);
echo '</pre>';


$GBPUSD = $forexdecode->"Realtime Currency Exchange Rate"->"5. Exchange Rate";?>'

I have no problems getting data printed using print_r. I get:

stdClass Object ( [Realtime Currency Exchange Rate] => stdClass Object ( [1. From_Currency Code] => USD [2. From_Currency Name] => United States Dollar [3. To_Currency Code] => JPY [4. To_Currency Name] => Japanese Yen [5. Exchange Rate] => 109.72200000 [6. Last Refreshed] => 2019-02-10 07:26:32 [7. Time Zone] => UTC ) )

but when I try to get data into variable $GBPUSD I get blank page.

How do I get data from this API?

 [5. Exchange Rate]
2
  • You need to use {} notation: $GBPUSD = $forexdecode->{"Realtime Currency Exchange Rate"}->{"5. Exchange Rate"}; Commented Feb 10, 2019 at 7:51
  • It worked. Thank you Commented Feb 10, 2019 at 7:56

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.