0

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?

Lux
  • 17,835
  • 5
  • 43
  • 73
  • I haven't written PHP code in a while, but are you sure `file_get_contents takes the `Content-Type` header into account and return an array? Maybe a type checking on `$r` would help. If it's a simple string it's quite clear what to do next. – Mohammad Jafar Mashhadi Mar 01 '20 at 23:06

1 Answers1

0

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);
Illya
  • 1,268
  • 1
  • 5
  • 16