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