3 require_once 'Pman.php';
5 class Pman_Core_UpdateCurrencyRate extends Pman
8 static $cli_desc = "Update Currency Exchange Rate";
10 static $cli_opts = array(
11 'procedures-only' => array(
12 'desc' => 'Only run procedures and return',
22 var $actionUrl = 'http://www.oanda.com/currency/historical-rates-classic';
30 $ff = HTML_FlexyFramework::get();
31 if (!empty($ff->cli)) {
39 function get($args, $opts)
44 $response = $this->curl($this->actionUrl, array(), 'GET');
46 libxml_use_internal_errors (true);
48 $doc = new DOMDocument();
49 $doc->loadHTML($response);
51 libxml_use_internal_errors (false);
53 $xpath = new DOMXpath($doc);
55 $elements = $xpath->query("//select[@name='exch']/option");
57 foreach($elements as $el) {
58 $currency[] = $el->getAttribute('value');
62 die('no any currency');
65 $fromDate = date('m/d/y', strtotime("-6 MONTH"));
66 $toDate = date('m/d/y');
68 $total = count($currency);
70 foreach ($currency as $k => $c){
72 echo "\nProcessing Currency : $c ($k / $total) \n";
86 $response = $this->curl($this->actionUrl, $params, 'POST');
88 libxml_use_internal_errors (true);
90 $doc = new DOMDocument();
91 $doc->loadHTML($response);
93 libxml_use_internal_errors (false);
95 $xpath = new DOMXpath($doc);
97 $elements = $xpath->query("//td[@id='content_section']/table/tr[last()]/td/table/tr[1]/td[last()]");
99 $rate = empty($elements->item(0)->nodeValue) ? 0 : $elements->item(0)->nodeValue * 1;
101 $this->processCurrRate($c, $rate, $fromDate, $toDate);
103 if(array_key_exists($c, $this->mapping)){
104 $this->processCurrRate($this->mapping[$c], $rate, $fromDate, $toDate);
112 function processCurrRate($currency, $rate, $from, $to)
114 $curr = DB_DataObject::factory('core_curr_rate');
116 $curr->curr = $currency;
120 if($curr->find(true)){
124 $curr->setFrom(array(
126 'from_dt' => date('Y-m-d H:i:s', strtotime($from)),
127 'to_dt' => date('Y-m-d H:i:s', strtotime($to))
130 (empty($o)) ? $curr->insert() : $curr->update($o);
135 function curl($url, $request = array(), $method = 'GET')
138 if(is_array($request)){
139 $request = http_build_query($request);
142 $url = $url . ($method == 'GET' ? "?" . $request : '');
143 $ch = curl_init($url);
145 if ($method == 'POST') {
146 curl_setopt($ch, CURLOPT_POST, 1);
147 curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
148 curl_setopt($ch, CURLOPT_HTTPHEADER,
149 array("Content-Type: application/x-www-form-urlencoded", "Content-Length: " . strlen($request)));
152 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
154 curl_setopt($ch, CURLOPT_HEADER, false);
155 curl_setopt($ch, CURLOPT_VERBOSE, 1);
156 curl_setopt($ch, CURLOPT_TIMEOUT, 30);
157 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
159 $response = curl_exec($ch);