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