UpdateCurrencyRate.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)
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 aobut 7.8)\n ";
44         
45         die("done");
46         
47         
48         
49         
50         
51         
52         
53     }
54     function oldversion() 
55     {
56         $currency = array();
57         
58         $response = $this->curl($this->actionUrl, array(), 'GET');
59         
60         libxml_use_internal_errors (true);
61         
62         $doc = new DOMDocument();
63         $doc->loadHTML($response);
64         
65         libxml_use_internal_errors (false);
66         
67         $xpath = new DOMXpath($doc);
68         
69         $elements = $xpath->query("//select[@name='exch']/option");
70         
71         foreach($elements as $el) {
72             $currency[] = $el->getAttribute('value');
73         }
74         
75         if(empty($currency)){
76             die('no any currency');
77         }
78         
79         $fromDate = date('m/d/y', strtotime("-6 MONTH"));
80         $toDate = date('m/d/y');
81         
82         $total = count($currency);
83         
84         foreach ($currency as $k => $c){
85             
86             echo "\nProcessing Currency : $c ($k / $total) \n";
87             
88             $params = array(
89                 'lang'          => 'en',
90                 'result'        => 1,
91                 'date1'         => $fromDate,
92                 'date'          => $toDate,
93                 'date_fmt'      => 'us',
94                 'exch'          => $c,
95                 'expr'          => 'USD',
96                 'margin_fixed'  => 0,
97                 'format'        => 'HTML'
98             );
99             
100             $response = $this->curl($this->actionUrl, $params, 'POST');
101         
102             libxml_use_internal_errors (true);
103
104             $doc = new DOMDocument();
105             $doc->loadHTML($response);
106
107             libxml_use_internal_errors (false);
108
109             $xpath = new DOMXpath($doc);
110
111             $elements = $xpath->query("//td[@id='content_section']/table/tr[last()]/td/table/tr[1]/td[last()]");
112
113             $rate = empty($elements->item(0)->nodeValue) ? 0 : $elements->item(0)->nodeValue * 1;
114
115             $this->processCurrRate($c, $rate, $fromDate, $toDate);
116             
117             if(array_key_exists($c, $this->mapping)){
118                 $this->processCurrRate($this->mapping[$c], $rate, $fromDate, $toDate);
119             }
120         }
121         
122         return;
123     }
124     
125     function processCurrRate($currency, $rate, $from, $to)
126     {
127         $curr = DB_DataObject::factory('core_curr_rate');
128             
129         $curr->curr = $currency;
130
131         $o = false;
132
133         if($curr->find(true)){
134             $o = clone($curr);
135         }
136
137         $curr->setFrom(array(
138             'rate'  => $rate,
139             'from_dt'  => date('Y-m-d H:i:s', strtotime($from)),
140             'to_dt'    => date('Y-m-d H:i:s', strtotime($to))
141         ));
142
143         (empty($o)) ? $curr->insert() : $curr->update($o);
144         
145         return;
146     }
147     
148     function curl($url, $request = array(), $method = 'GET') 
149     {
150          
151         if(is_array($request)){
152             $request = http_build_query($request);
153         }
154         
155         $url = $url . ($method == 'GET' ? "?" . $request : '');  
156         $ch = curl_init($url);
157         
158         if ($method == 'POST') {
159             curl_setopt($ch, CURLOPT_POST, 1);
160             curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
161             curl_setopt($ch, CURLOPT_HTTPHEADER,
162                     array("Content-Type: application/x-www-form-urlencoded", "Content-Length: " . strlen($request)));
163         }
164         
165         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
166         
167         curl_setopt($ch, CURLOPT_HEADER, false);
168         curl_setopt($ch, CURLOPT_VERBOSE, 1);
169         curl_setopt($ch, CURLOPT_TIMEOUT, 30);
170         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
171
172         $response = curl_exec($ch);
173
174         curl_close($ch);
175         
176         return $response;
177     }
178     
179 }