Joel's Thoughts

Open Exchange Rates API - PHP

July 13, 2016

Open Exchange Rates offers a data currency API to interact with your application. I used their API on the Currency Converter application I built.

Screen shot of Currency Converter application.

Today, I’m going to write a PHP class that will interact with Open Exchange Rate’s API and will return a key-value pair array of currency rates.

    class OpenExchange {

        protected $app_id;


        public function __construct($app_id){
            $this->app_id = $app_id;
        }

    }

We start with declaring the $app_id. Open Exchange Rates will provide you with an “Application ID” needed to call their API. Instantiating the OpenExchange class will require a value for $app_id.

Next, I’m going to create a function that will actually call the current currency exchange rates.

    class OpenExchange {

            protected $app_id;


            public function __construct($app_id){
                $this->app_id = $app_id;
            }

            public function getRates(){        
                $app_id = $this->app_id;
                $file = "latest.json";  
                header("Content-Type: application/json");
                $json = file_get_contents("http://openexchangerates.org/api/{$file}?app_id={$app_id}");
                $obj = json_decode($json);
                $rate_container = array();

                if(isset($obj->{"rates"})){
                    foreach($obj->{"rates"} as $key=>$rate){
                        $rate_container[$key]=$rate;
                    }
                }

                return $rate_container;
            }   

    }

Using PHP’s file_get_contents function, getRates will call the Open Exchange Rate API that returns a JSON format. Then it will validate the returned JSON data if it has a “rate” property and is not empty. If validation passes, function will loop through each of the “rate” items key-value pair to store them in a key-value PHP array $rate_container.

I use the OpenExchange class like this,

   $app_id = "my_open_exchange_rate_key";
   $open_exchange = new OpenExchange($app_id);
   $current_rates = $open_exchange->getRates();

Printing out the returned array on the console,


    Array
    (
        [AED] => 3.67
        [AFN] => 68.56
        [ALL] => 122.8861
        [AMD] => 476.506667
        ..................... more 
    )

Next step would be to use this freshly fetch data. I could update stored currency rates on my database using this function.


    public function updateCurrencyRates($conn,$current_rates){
        $conn->beginTransaction();        
        try{

            foreach($current_rates as $currency_code=>$rate){
                $code = trim(strtolower($currency_code));
                $count = $conn->executeUpdate("UPDATE currencies SET rate = ? WHERE TRIM(LOWER(code)) = ?", array($rate, $code));
                if($count > 0){
                   echo "Updated:".$currency_code." = ".$rate."\n";
                }
                else{
                    echo  "No update:".$currency_code."\n"; 
                }
            }       

            $conn->commit();

            return "Completed update process.";

        }catch(\Exception $e){         
          print_r($e);         
        } 
    }

Check-out the whole OpenExchange class here.









  • About
  • Search
  • Resume
  • Powered by Jekyll using the Trio theme