how it works this currency converter api, get rate?

90 Views Asked by At

I'm trying to use this api (currency exchange api). This returns something like this:

{
  "from":"USD",
  "to":"HKD",
  "rate":7.7950999999999996958877090946771204471588134765625
}

I send the request with file_get_contents() and this seems to work. My problem is that I can't access the array key rate.

$r = file_get_contents('https://cc.hirak.cc/usd_hkd');
print_r($r['rate']); // nothing is shown. This is the problem.
print_r($r);  // result shows ( all the array ) 

How can I access just the rate key?

1

There are 1 best solutions below

0
Illya On

you are accessing an string instead of array, Try this code, I think this will work:

$r = json_encode(file_get_contents('https://cc.hirak.cc/usd_hkd'),true);