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