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();
14 var $actionUrl = 'http://www.oanda.com/currency/historical-rates-classic';
22 $ff = HTML_FlexyFramework::get();
23 if (!empty($ff->cli)) {
31 function get($args, $opts)
35 $response = $this->curl($this->actionUrl, array(), 'GET');
37 libxml_use_internal_errors (true);
39 $doc = new DOMDocument();
40 $doc->loadHTML($response);
42 libxml_use_internal_errors (false);
44 $xpath = new DOMXpath($doc);
46 $elements = $xpath->query("//select[@name='exch']/option");
48 foreach($elements as $el) {
49 $currency[] = $el->getAttribute('value');
53 die('no any currency');
56 $fromDate = date('m/d/y', strtotime("-6 MONTH"));
57 $toDate = date('m/d/y');
59 $total = count($currency);
61 foreach ($currency as $k => $c){
63 echo "\nProcessing Currency : $c ($k / $total) \n";
77 $response = $this->curl($this->actionUrl, $params, 'POST');
79 libxml_use_internal_errors (true);
81 $doc = new DOMDocument();
82 $doc->loadHTML($response);
84 libxml_use_internal_errors (false);
86 $xpath = new DOMXpath($doc);
88 $elements = $xpath->query("//td[@id='content_section']/table/tr[last()]/td/table/tr[1]/td[last()]");
90 $rate = empty($elements->item(0)->nodeValue) ? 0 : $elements->item(0)->nodeValue * 1;
92 $this->processCurrRate($c, $rate, $fromDate, $toDate);
94 if(array_key_exists($c, $this->mapping)){
95 $this->processCurrRate($this->mapping[$c], $rate, $fromDate, $toDate);
102 function processCurrRate($currency, $rate, $from, $to)
104 $curr = DB_DataObject::factory('core_curr_rate');
106 $curr->curr = $currency;
110 if($curr->find(true)){
114 $curr->setFrom(array(
116 'from_dt' => date('Y-m-d H:i:s', strtotime($from)),
117 'to_dt' => date('Y-m-d H:i:s', strtotime($to))
120 (empty($o)) ? $curr->insert() : $curr->update($o);
125 function curl($url, $request = array(), $method = 'GET')
128 if(is_array($request)){
129 $request = http_build_query($request);
132 $url = $url . ($method == 'GET' ? "?" . $request : '');
133 $ch = curl_init($url);
135 if ($method == 'POST') {
136 curl_setopt($ch, CURLOPT_POST, 1);
137 curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
138 curl_setopt($ch, CURLOPT_HTTPHEADER,
139 array("Content-Type: application/x-www-form-urlencoded", "Content-Length: " . strlen($request)));
142 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
144 curl_setopt($ch, CURLOPT_HEADER, false);
145 curl_setopt($ch, CURLOPT_VERBOSE, 1);
146 curl_setopt($ch, CURLOPT_TIMEOUT, 30);
147 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
149 $response = curl_exec($ch);