3 // note this no longer works -- we could try using their JSON feed?
6 require_once 'Pman.php';
8 class Pman_Core_UpdateCurrencyRate extends Pman
11 static $cli_desc = "Update Currency Exchange Rate";
13 static $cli_opts = array();
17 var $actionUrl = 'http://www.oanda.com/currency/historical-rates-classic';
25 $ff = HTML_FlexyFramework::get();
26 if (!empty($ff->cli)) {
34 function get($args, $opts)
37 DB_DataObject::DebugLevel(1);
38 $d = DB_DataObject::Factory('core_curr_rate');
53 $response = $this->curl($this->actionUrl, array(), 'GET');
55 libxml_use_internal_errors (true);
57 $doc = new DOMDocument();
58 $doc->loadHTML($response);
60 libxml_use_internal_errors (false);
62 $xpath = new DOMXpath($doc);
64 $elements = $xpath->query("//select[@name='exch']/option");
66 foreach($elements as $el) {
67 $currency[] = $el->getAttribute('value');
71 die('no any currency');
74 $fromDate = date('m/d/y', strtotime("-6 MONTH"));
75 $toDate = date('m/d/y');
77 $total = count($currency);
79 foreach ($currency as $k => $c){
81 echo "\nProcessing Currency : $c ($k / $total) \n";
95 $response = $this->curl($this->actionUrl, $params, 'POST');
97 libxml_use_internal_errors (true);
99 $doc = new DOMDocument();
100 $doc->loadHTML($response);
102 libxml_use_internal_errors (false);
104 $xpath = new DOMXpath($doc);
106 $elements = $xpath->query("//td[@id='content_section']/table/tr[last()]/td/table/tr[1]/td[last()]");
108 $rate = empty($elements->item(0)->nodeValue) ? 0 : $elements->item(0)->nodeValue * 1;
110 $this->processCurrRate($c, $rate, $fromDate, $toDate);
112 if(array_key_exists($c, $this->mapping)){
113 $this->processCurrRate($this->mapping[$c], $rate, $fromDate, $toDate);
120 function processCurrRate($currency, $rate, $from, $to)
122 $curr = DB_DataObject::factory('core_curr_rate');
124 $curr->curr = $currency;
128 if($curr->find(true)){
132 $curr->setFrom(array(
134 'from_dt' => date('Y-m-d H:i:s', strtotime($from)),
135 'to_dt' => date('Y-m-d H:i:s', strtotime($to))
138 (empty($o)) ? $curr->insert() : $curr->update($o);
143 function curl($url, $request = array(), $method = 'GET')
146 if(is_array($request)){
147 $request = http_build_query($request);
150 $url = $url . ($method == 'GET' ? "?" . $request : '');
151 $ch = curl_init($url);
153 if ($method == 'POST') {
154 curl_setopt($ch, CURLOPT_POST, 1);
155 curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
156 curl_setopt($ch, CURLOPT_HTTPHEADER,
157 array("Content-Type: application/x-www-form-urlencoded", "Content-Length: " . strlen($request)));
160 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
162 curl_setopt($ch, CURLOPT_HEADER, false);
163 curl_setopt($ch, CURLOPT_VERBOSE, 1);
164 curl_setopt($ch, CURLOPT_TIMEOUT, 30);
165 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
167 $response = curl_exec($ch);