SimpleExcel.php
[Pman.Core] / UpdateCurrencyRate.php
1 <?php
2
3 // note this no longer works -- we could try using their JSON feed?
4
5
6 require_once 'Pman.php';
7
8 class Pman_Core_UpdateCurrencyRate extends Pman
9 {
10     
11     static $cli_desc = "Update Currency Exchange Rate";
12     
13     static $cli_opts = array();
14     
15     var $cli = false; 
16     
17     var $actionUrl = 'http://www.oanda.com/currency/historical-rates-classic';
18     
19     var $mapping = array(
20         'CNY' => 'RMB'
21     );
22     
23     function getAuth() 
24     {
25         $ff = HTML_FlexyFramework::get();
26         if (!empty($ff->cli)) {
27             $this->cli = true;
28             return true;
29         }
30         
31         die("NOT ALLOWED");
32     }
33     
34     function get($args, $opts=array())
35     {   
36         
37         //DB_DataObject::DebugLevel(1);
38         $d = DB_DataObject::Factory('core_curr_rate');
39         $d->loadRates();
40         
41         $hkd = $d->convert(1,'USD','HKD');
42         
43         echo "1 USD is ~ $hkd HKD (should be about 7.75)\n ";
44         
45         die("done");
46         
47          
48         
49     }
50     function oldversion() 
51     {
52         $currency = array();
53         
54         $response = $this->curl($this->actionUrl, array(), 'GET');
55         
56         libxml_use_internal_errors (true);
57         
58         $doc = new DOMDocument();
59         $doc->loadHTML($response);
60         
61         libxml_use_internal_errors (false);
62         
63         $xpath = new DOMXpath($doc);
64         
65         $elements = $xpath->query("//select[@name='exch']/option");
66         
67         foreach($elements as $el) {
68             $currency[] = $el->getAttribute('value');
69         }
70         
71         if(empty($currency)){
72             die('no any currency');
73         }
74         
75         $fromDate = date('m/d/y', strtotime("-6 MONTH"));
76         $toDate = date('m/d/y');
77         
78         $total = count($currency);
79         
80         foreach ($currency as $k => $c){
81             
82             echo "\nProcessing Currency : $c ($k / $total) \n";
83             
84             $params = array(
85                 'lang'          => 'en',
86                 'result'        => 1,
87                 'date1'         => $fromDate,
88                 'date'          => $toDate,
89                 'date_fmt'      => 'us',
90                 'exch'          => $c,
91                 'expr'          => 'USD',
92                 'margin_fixed'  => 0,
93                 'format'        => 'HTML'
94             );
95             
96             $response = $this->curl($this->actionUrl, $params, 'POST');
97         
98             libxml_use_internal_errors (true);
99
100             $doc = new DOMDocument();
101             $doc->loadHTML($response);
102
103             libxml_use_internal_errors (false);
104
105             $xpath = new DOMXpath($doc);
106
107             $elements = $xpath->query("//td[@id='content_section']/table/tr[last()]/td/table/tr[1]/td[last()]");
108
109             $rate = empty($elements->item(0)->nodeValue) ? 0 : $elements->item(0)->nodeValue * 1;
110
111             $this->processCurrRate($c, $rate, $fromDate, $toDate);
112             
113             if(array_key_exists($c, $this->mapping)){
114                 $this->processCurrRate($this->mapping[$c], $rate, $fromDate, $toDate);
115             }
116         }
117         
118         return;
119     }
120     
121     function processCurrRate($currency, $rate, $from, $to)
122     {
123         $curr = DB_DataObject::factory('core_curr_rate');
124             
125         $curr->curr = $currency;
126
127         $o = false;
128
129         if($curr->find(true)){
130             $o = clone($curr);
131         }
132
133         $curr->setFrom(array(
134             'rate'  => $rate,
135             'from_dt'  => date('Y-m-d H:i:s', strtotime($from)),
136             'to_dt'    => date('Y-m-d H:i:s', strtotime($to))
137         ));
138
139         (empty($o)) ? $curr->insert() : $curr->update($o);
140         
141         return;
142     }
143     
144     function curl($url, $request = array(), $method = 'GET') 
145     {
146          
147         if(is_array($request)){
148             $request = http_build_query($request);
149         }
150         
151         $url = $url . ($method == 'GET' ? "?" . $request : '');  
152         $ch = curl_init($url);
153         
154         if ($method == 'POST') {
155             curl_setopt($ch, CURLOPT_POST, 1);
156             curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
157             curl_setopt($ch, CURLOPT_HTTPHEADER,
158                     array("Content-Type: application/x-www-form-urlencoded", "Content-Length: " . strlen($request)));
159         }
160         
161         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
162         
163         curl_setopt($ch, CURLOPT_HEADER, false);
164         curl_setopt($ch, CURLOPT_VERBOSE, 1);
165         curl_setopt($ch, CURLOPT_TIMEOUT, 30);
166         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
167
168         $response = curl_exec($ch);
169
170         curl_close($ch);
171         
172         return $response;
173     }
174     
175 }