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             if($k == 5){
107                 break;
108             }
109         }
110 //        
111 //        if(empty($opts['procedures-only'])){
112 //            $this->jok("DONE");
113 //        }
114 //        
115         return;
116     }
117     
118     function processCurrRate($currency, $rate, $from, $to)
119     {
120         $curr = DB_DataObject::factory('core_curr_rate');
121             
122         $curr->curr = $currency;
123
124         $o = false;
125
126         if($curr->find(true)){
127             $o = clone($curr);
128         }
129
130         $curr->setFrom(array(
131             'rate'  => $rate,
132             'from_dt'  => date('Y-m-d H:i:s', strtotime($from)),
133             'to_dt'    => date('Y-m-d H:i:s', strtotime($to))
134         ));
135
136         (empty($o)) ? $curr->insert() : $curr->update($o);
137         
138         return;
139     }
140     
141     function curl($url, $request = array(), $method = 'GET') 
142     {
143          
144         if(is_array($request)){
145             $request = http_build_query($request);
146         }
147         
148         $url = $url . ($method == 'GET' ? "?" . $request : '');  
149         $ch = curl_init($url);
150         
151         if ($method == 'POST') {
152             curl_setopt($ch, CURLOPT_POST, 1);
153             curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
154             curl_setopt($ch, CURLOPT_HTTPHEADER,
155                     array("Content-Type: application/x-www-form-urlencoded", "Content-Length: " . strlen($request)));
156         }
157         
158         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
159         
160         curl_setopt($ch, CURLOPT_HEADER, false);
161         curl_setopt($ch, CURLOPT_VERBOSE, 1);
162         curl_setopt($ch, CURLOPT_TIMEOUT, 30);
163         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
164
165         $response = curl_exec($ch);
166
167         curl_close($ch);
168         
169         return $response;
170     }
171     
172 }