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