How can I get foreign exchange rates thru an API by using PHP or Ajax?

1.6k views Asked by At

I need a foreign exchange rate API in PHP for my recent project, in which I need updates after 60 sec.I also need metals like Gold, Silver etc latest exchange rates. So anyone who can I guide me for a PHP endpoint providing latest exchange rates. Any help will be highly appreciated.

1

There are 1 answers

0
Rameez On BEST ANSWER

I'll strongly recommend you CurrencyFreaks API. It provides foreign currency exchange rate endpoint in multiple languages including PHP and Ajax. It's prominent features are:

  • Data is updated every 60 sec.
  • You can change the ‘Base’ currency.
  • It provides currency exchange rates for 179 currencies worldwide including currencies, metals (Gold, Silver, Palladium, Platinum), and cryptocurrencies.
  • Supported codes for the endpoints are Shell, Node.js, Java, Python, PHP, Ruby, JS, C#, Go, C, Swift.

Here's the latest exchange rates endpoint using PHP:

setUrl('https://api.currencyfreaks.com/latest
    ?apikey=YOUR_APIKEY
    &base=GBP
    &symbols=EUR,USD,PKR,INR');
$request->setMethod(HTTP_Request2::METHOD_GET);
$request->setConfig(array(
  'follow_redirects' => TRUE
));
try {
  $response = $request->send();
  if ($response->getStatus() == 200) {
    echo $response->getBody();
  }
  else {
    echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
    $response->getReasonPhrase();
  }
}
catch(HTTP_Request2_Exception $e) {
  echo 'Error: ' . $e->getMessage();
}

For Ajax:

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function() {
  if(this.readyState === 4) {
    console.log(this.responseText);
  }
});

xhr.open("GET", "https://api.currencyfreaks.com/latest
    ?apikey=YOUR_APIKEY
    &base=GBP
    &symbols=EUR,USD,PKR,INR");

xhr.send();

You can also get foreign exchange rate endpoint in other programming languages from here: https://currencyfreaks.com/documentation.html#Latest

I hope this solution will help for your recent project.