3 // note this no longer works
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)
38 $response = $this->curl($this->actionUrl, array(), 'GET');
40 libxml_use_internal_errors (true);
42 $doc = new DOMDocument();
43 $doc->loadHTML($response);
45 libxml_use_internal_errors (false);
47 $xpath = new DOMXpath($doc);
49 $elements = $xpath->query("//select[@name='exch']/option");
51 foreach($elements as $el) {
52 $currency[] = $el->getAttribute('value');
56 die('no any currency');
59 $fromDate = date('m/d/y', strtotime("-6 MONTH"));
60 $toDate = date('m/d/y');
62 $total = count($currency);
64 foreach ($currency as $k => $c){
66 echo "\nProcessing Currency : $c ($k / $total) \n";
80 $response = $this->curl($this->actionUrl, $params, 'POST');
82 libxml_use_internal_errors (true);
84 $doc = new DOMDocument();
85 $doc->loadHTML($response);
87 libxml_use_internal_errors (false);
89 $xpath = new DOMXpath($doc);
91 $elements = $xpath->query("//td[@id='content_section']/table/tr[last()]/td/table/tr[1]/td[last()]");
93 $rate = empty($elements->item(0)->nodeValue) ? 0 : $elements->item(0)->nodeValue * 1;
95 $this->processCurrRate($c, $rate, $fromDate, $toDate);
97 if(array_key_exists($c, $this->mapping)){
98 $this->processCurrRate($this->mapping[$c], $rate, $fromDate, $toDate);
105 function processCurrRate($currency, $rate, $from, $to)
107 $curr = DB_DataObject::factory('core_curr_rate');
109 $curr->curr = $currency;
113 if($curr->find(true)){
117 $curr->setFrom(array(
119 'from_dt' => date('Y-m-d H:i:s', strtotime($from)),
120 'to_dt' => date('Y-m-d H:i:s', strtotime($to))
123 (empty($o)) ? $curr->insert() : $curr->update($o);
128 function curl($url, $request = array(), $method = 'GET')
131 if(is_array($request)){
132 $request = http_build_query($request);
135 $url = $url . ($method == 'GET' ? "?" . $request : '');
136 $ch = curl_init($url);
138 if ($method == 'POST') {
139 curl_setopt($ch, CURLOPT_POST, 1);
140 curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
141 curl_setopt($ch, CURLOPT_HTTPHEADER,
142 array("Content-Type: application/x-www-form-urlencoded", "Content-Length: " . strlen($request)));
145 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
147 curl_setopt($ch, CURLOPT_HEADER, false);
148 curl_setopt($ch, CURLOPT_VERBOSE, 1);
149 curl_setopt($ch, CURLOPT_TIMEOUT, 30);
150 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
152 $response = curl_exec($ch);