/** * Loads all currency rate data from service provider * * @access protected * @param string $from_currency A string name of currency available in static::$currencies * @return void */ protected function fetch_currency_rate($from_currency) { Cache::forge('hybrid.currency.' . $from_currency, Config::get('hybrid.currency.cache', array())); if (!array_key_exists($from_currency, static::$currencies)) { throw new FuelException(__METHOD__ . ": Unable to use unknown currency {$from_currency}"); } try { $this->currency_rates = Cache::get('hybrid.currency.' . $from_currency); } catch (CacheNotFoundException $e) { $search = array('{AMOUNT}', '{FROM}', '{TO}'); // load data for each currency, this might take awhile foreach (static::$currencies as $cur => $name) { $replace = array('1', $from_currency, $cur); $url = str_replace($search, $replace, static::$service); try { $data = Curl::get($url)->setopt(array(CURLOPT_BINARYTRANSFER => 1, CURLOPT_RETURNTRANSFER => true, CURLOPT_MAXREDIRS => 5, CURLOPT_HEADER => 0, CURLOPT_USERAGENT => "Fuel PHP framework - \\Hybrid\\Currency class"))->execute(); $body = $data->body; } catch (FuelException $e) { $body = file_get_contents($url); } // this is rather hackish, the return body from either Curl or file_get_contents can't be use directly with json_decode foreach (array("lhs", "rhs", "error", "icc") as $key) { $body = str_replace($key . ":", '"' . $key . '":', $body); } // need to decode this first $data = json_decode($body); if (null !== $data and false !== $data->icc) { $conversion = \Format::forge($body, 'json')->to_array(); $tmp = explode(' ', $conversion['rhs']); $rate = array_shift($tmp); $this->currency_rates[$cur] = (double) $rate; } } Cache::set('hybrid.currency.' . $from_currency, $this->currency_rates); } }
/** * Send a curl request to Hipchat * * @static * @access protected * @param string $url * @param array $data * @return Hybrid\Curl */ protected static function call($url, $data) { $request = Curl::make($url, $data); $request->option(CURLOPT_SSL_VERIFYPEER, Config::get('hipchat::api.verify_ssl', 1)); $request->option(CURLOPT_FOLLOWLOCATION, 1); $request->option(CURLOPT_RETURNTRANSFER, 1); $request->option(CURLOPT_TIMEOUT, 15); return $request->call(); }