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)) {
33 echo "obtain the currency lists... \n";
37 $response = $this->curl($this->actionUrl, array(), 'GET');
39 libxml_use_internal_errors (true);
41 $doc = new DOMDocument();
42 $doc->loadHTML($response);
44 libxml_use_internal_errors (false);
46 $xpath = new DOMXpath($doc);
48 $elements = $xpath->query("//select[@name='exch']/option");
50 foreach($elements as $el) {
51 $currency[] = $el->getAttribute('value');
55 die('no any currency');
58 $fromDate = date('m/d/y', strtotime("-6 MONTH"));
59 $toDate = date('m/d/y');
61 foreach ($currency as $c){
74 $response = $this->curl($this->actionUrl, $params, 'POST');
76 libxml_use_internal_errors (true);
78 $doc = new DOMDocument();
79 $doc->loadHTML($response);
81 libxml_use_internal_errors (false);
83 $xpath = new DOMXpath($doc);
85 $elements = $xpath->query("//td[@id='content_section']/table/tr[last()]/td/table/tr[1]/td[last()]");
87 $rate = empty($elements->item(0)->nodeValue) ? 0 : $elements->item(0)->nodeValue * 1;
89 $curr = DB_DataObject::factory('core_curr_rate');
95 if($curr->find(true)){
101 'from' => date('Y-m-d H:i:s', strtotime($fromDate)),
102 'to' => date('Y-m-d H:i:s', strtotime($toDate))
105 (empty($o)) ? $curr->insert() ? $curr->update($o);
112 function curl($url, $request = array(), $method = 'GET')
115 if(is_array($request)){
116 $request = http_build_query($request);
119 $url = $url . ($method == 'GET' ? "?" . $request : '');
120 $ch = curl_init($url);
122 if ($method == 'POST') {
123 curl_setopt($ch, CURLOPT_POST, 1);
124 curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
125 curl_setopt($ch, CURLOPT_HTTPHEADER,
126 array("Content-Type: application/x-www-form-urlencoded", "Content-Length: " . strlen($request)));
129 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
131 curl_setopt($ch, CURLOPT_HEADER, false);
132 curl_setopt($ch, CURLOPT_VERBOSE, 1);
133 curl_setopt($ch, CURLOPT_TIMEOUT, 30);
134 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
136 $response = curl_exec($ch);