UpdateCurrencyRate.php
[Pman.Core] / UpdateCurrencyRate.php
1 <?php
2
3 require_once 'Pman.php';
4
5 class Pman_Core_UpdateCurrencyRate extends Pman
6 {
7     
8     static $cli_desc = "Update Currency Exchange Rate";
9     
10     static $cli_opts = array();
11     
12     var $cli = false; 
13     
14     var $actionUrl = 'http://www.oanda.com/currency/historical-rates-classic';
15     
16     var $mapping = array(
17         'CNY' => 'RMB'
18     );
19     
20     function getAuth() 
21     {
22         $ff = HTML_FlexyFramework::get();
23         if (!empty($ff->cli)) {
24             $this->cli = true;
25             return true;
26         }
27         
28         die("NOT ALLOWED");
29     }
30     
31     function get()
32     {   
33         $currency = array();
34         
35         $response = $this->curl($this->actionUrl, array(), 'GET');
36         
37         libxml_use_internal_errors (true);
38         
39         $doc = new DOMDocument();
40         $doc->loadHTML($response);
41         
42         libxml_use_internal_errors (false);
43         
44         $xpath = new DOMXpath($doc);
45         
46         $elements = $xpath->query("//select[@name='exch']/option");
47         
48         foreach($elements as $el) {
49             $currency[] = $el->getAttribute('value');
50         }
51         
52         if(empty($currency)){
53             die('no any currency');
54         }
55         
56         $fromDate = date('m/d/y', strtotime("-6 MONTH"));
57         $toDate = date('m/d/y');
58         
59         $total = count($currency);
60         
61         foreach ($currency as $k => $c){
62             
63             echo "\nProcessing Currency : $c        ($k / $total) \n";
64             
65             $params = array(
66                 'lang'          => 'en',
67                 'result'        => 1,
68                 'date1'         => $fromDate,
69                 'date'          => $toDate,
70                 'date_fmt'      => 'us',
71                 'exch'          => $c,
72                 'expr'          => 'USD',
73                 'margin_fixed'  => 0,
74                 'format'        => 'HTML'
75             );
76             
77             $response = $this->curl($this->actionUrl, $params, 'POST');
78         
79             libxml_use_internal_errors (true);
80
81             $doc = new DOMDocument();
82             $doc->loadHTML($response);
83
84             libxml_use_internal_errors (false);
85
86             $xpath = new DOMXpath($doc);
87
88             $elements = $xpath->query("//td[@id='content_section']/table/tr[last()]/td/table/tr[1]/td[last()]");
89
90             $rate = empty($elements->item(0)->nodeValue) ? 0 : $elements->item(0)->nodeValue * 1;
91
92             
93             
94             if(array_key_exists($c, $this->mapping)){
95                 
96                 $curr = DB_DataObject::factory('core_curr_rate');
97             
98                 $curr->curr = $c;
99
100                 $o = false;
101
102                 if($curr->find(true)){
103                     $o = clone($curr);
104                 }
105
106                 $curr->setFrom(array(
107                     'rate'  => $rate,
108                     'from_dt'  => date('Y-m-d H:i:s', strtotime($fromDate)),
109                     'to_dt'    => date('Y-m-d H:i:s', strtotime($toDate))
110                 ));
111
112                 (empty($o)) ? $curr->insert() : $curr->update($o);
113             }
114             
115         }
116         
117         $this->jok("DONE");
118         
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     /*
176     lang:en
177     result:1
178     date1:10/14/14
179     date:10/20/14
180     date_fmt:us
181     exch:USD
182     exch2:
183     expr:EUR
184     expr2:
185     margin_fixed:0
186     format:HTML
187     SUBMIT:Get Table
188     */
189 }