Skip to content

Instantly share code, notes, and snippets.

@wzul
Created August 16, 2019 17:58
Show Gist options
  • Save wzul/7b72a4284e4c2b3230c688cad72a6772 to your computer and use it in GitHub Desktop.
Save wzul/7b72a4284e4c2b3230c688cad72a6772 to your computer and use it in GitHub Desktop.
[PHP] BNM API Get Latest Exchange Rate
<?php
$host = 'https://api.bnm.gov.my/public/exchange-rate';
$header = 'Accept: application/vnd.BNM.API.v1+json';
$from_currency = 'USD';
$data = array(
'session' => "0900",
);
$process = curl_init($host . '?' . http_build_query($data));
curl_setopt($process, CURLOPT_HEADER, 0);
curl_setopt($process, CURLOPT_TIMEOUT, 5);
curl_setopt($process, CURLOPT_HTTPHEADER, array($header));
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($process, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($process, CURLOPT_SSL_VERIFYPEER, 0);
$return = curl_exec($process);
curl_close($process);
$arr = json_decode($return, true);
foreach ($arr['data'] as $currency) {
if (in_array($from_currency, $currency)) {
$data = $currency;
break;
}
}
echo print_r($data, true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment