| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * A PHP SDK to communicate with the open exchange rates APIs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @author  Dan Belden <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @license https://github.com/danbelden/open-exchange-rates/blob/master/licence.txt MIT License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace OpenExRt; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class Client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     // Client constants | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     const API_URL_ENDPOINT      = 'http://openexchangerates.org/api/'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     const API_FILE_LATEST       = 'latest.json'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     const API_FILE_CURRENCIES   = 'currencies.json'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     const API_FOLDER_HISTORICAL = 'historical'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     const OPTION_APP_ID         = 'appId'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * The App ID required to enable connectivity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @see https://openexchangerates.org/documentation#app-ids | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private $appId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * Client constructor method to initialise the client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     public function __construct($options = array()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         // If the app Id is provided to the constructor, set it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         if (isset($options[self::OPTION_APP_ID])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             $this->setAppId($options[self::OPTION_APP_ID]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * Getter method to retrieve the configured App ID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @see    https://openexchangerates.org/documentation#app-ids | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     public function getAppId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         return $this->appId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * Setter method to configure the client App ID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * - Login to your account to retrieve your App Id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @see    https://openexchangerates.org/documentation#app-ids | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param string $appId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @return OpenExchange\Client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     public function setAppId($appId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         // Set the app ID provided | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $this->appId = $appId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         // Enable method chaining by returning this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * Method to retrieve the latest currency rate information | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @see    https://openexchangerates.org/documentation#accessing-the-api | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @return stdClass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     public function getLatest() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         // Form the url to the API for the request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $apiUrl = sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             '%s%s?app_id=%s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             self::API_URL_ENDPOINT, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             self::API_FILE_LATEST, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $this->getAppId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         // Retrieve the API response in decoded object form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         return $this->getCurlResponse($apiUrl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * Method to retrieve the currency list available from the API | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @see    https://openexchangerates.org/documentation#accessing-the-api | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @return stdClass | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 98 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |     public function getCurrencies() | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |         // Form the url to the API for the request | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         $apiUrl = sprintf( | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |             '%s%s?app_id=%s', | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |             self::API_URL_ENDPOINT, | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |             self::API_FILE_CURRENCIES, | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |             $this->getAppId() | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |         // Retrieve the API response in decoded object form | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |         return $this->getCurlResponse($apiUrl); | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * Method to retrieve the historical rate information for a given date | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @see https://openexchangerates.org/documentation#historical-data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * @param \DateTime $date | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @return stdClass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     public function getHistorical(\DateTime $date) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         // Form the url to the API for the request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $apiUrl = sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             '%s%s?app_id=%s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             self::API_URL_ENDPOINT, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             $this->getHistoricalFilePath($date), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             $this->getAppId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         // Retrieve the API response in decoded object form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         return $this->getCurlResponse($apiUrl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * Helper function to retrieve the relative file path to historical rate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * json files on the open exchange server. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * @param  \DateTime $date | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     private function getHistoricalFilePath(\DateTime $date) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         return self::API_FOLDER_HISTORICAL . '/' . $date->format('Y-m-d') . '.json'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * Helper method to perform a cURL request to the API for a given URL's data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * @param string $url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @return stdClass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     private function getCurlResponse($url) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         // Open CURL session ensuring the correct headers are configured | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         $curlHandler = curl_init($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         // Retrieve the JSON response from the API | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         $json = curl_exec($curlHandler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         curl_close($curlHandler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         // Decode JSON response and return it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         return json_decode($json); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 165 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 166 |  |  |  |