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)
43 $response = $this->curl($this->actionUrl, array(), 'GET');
45 libxml_use_internal_errors (true);
47 $doc = new DOMDocument();
48 $doc->loadHTML($response);
50 libxml_use_internal_errors (false);
52 $xpath = new DOMXpath($doc);
54 $elements = $xpath->query("//select[@name='exch']/option");
56 foreach($elements as $el) {
57 $currency[] = $el->getAttribute('value');
61 die('no any currency');
64 $fromDate = date('m/d/y', strtotime("-6 MONTH"));
65 $toDate = date('m/d/y');
67 $total = count($currency);
69 foreach ($currency as $k => $c){
71 echo "\nProcessing Currency : $c ($k / $total) \n";
85 $response = $this->curl($this->actionUrl, $params, 'POST');
87 libxml_use_internal_errors (true);
89 $doc = new DOMDocument();
90 $doc->loadHTML($response);
92 libxml_use_internal_errors (false);
94 $xpath = new DOMXpath($doc);
96 $elements = $xpath->query("//td[@id='content_section']/table/tr[last()]/td/table/tr[1]/td[last()]");
98 $rate = empty($elements->item(0)->nodeValue) ? 0 : $elements->item(0)->nodeValue * 1;
100 $this->processCurrRate($c, $rate, $fromDate, $toDate);
102 if(array_key_exists($c, $this->mapping)){
103 $this->processCurrRate($this->mapping[$c], $rate, $fromDate, $toDate);
111 function processCurrRate($currency, $rate, $from, $to)
113 $curr = DB_DataObject::factory('core_curr_rate');
115 $curr->curr = $currency;
119 if($curr->find(true)){
123 $curr->setFrom(array(
125 'from_dt' => date('Y-m-d H:i:s', strtotime($from)),
126 'to_dt' => date('Y-m-d H:i:s', strtotime($to))
129 (empty($o)) ? $curr->insert() : $curr->update($o);
134 function curl($url, $request = array(), $method = 'GET')
137 if(is_array($request)){
138 $request = http_build_query($request);
141 $url = $url . ($method == 'GET' ? "?" . $request : '');
142 $ch = curl_init($url);
144 if ($method == 'POST') {
145 curl_setopt($ch, CURLOPT_POST, 1);
146 curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
147 curl_setopt($ch, CURLOPT_HTTPHEADER,
148 array("Content-Type: application/x-www-form-urlencoded", "Content-Length: " . strlen($request)));
151 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
153 curl_setopt($ch, CURLOPT_HEADER, false);
154 curl_setopt($ch, CURLOPT_VERBOSE, 1);
155 curl_setopt($ch, CURLOPT_TIMEOUT, 30);
156 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
158 $response = curl_exec($ch);