apicurrencyforexcurrency-exchange-rates

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


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.


Solution

  • 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:

    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.