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