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