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         foreach ($currency as $k => $c){
62             $params = array(
63                 'lang'          => 'en',
64                 'result'        => 1,
65                 'date1'         => $fromDate,
66                 'date'          => $toDate,
67                 'date_fmt'      => 'us',
68                 'exch'          => $c,
69                 'expr'          => 'USD',
70                 'margin_fixed'  => 0,
71                 'format'        => 'HTML'
72             );
73             
74             $response = $this->curl($this->actionUrl, $params, 'POST');
75         
76             libxml_use_internal_errors (true);
77
78             $doc = new DOMDocument();
79             $doc->loadHTML($response);
80
81             libxml_use_internal_errors (false);
82
83             $xpath = new DOMXpath($doc);
84
85             $elements = $xpath->query("//td[@id='content_section']/table/tr[last()]/td/table/tr[1]/td[last()]");
86
87             $rate = empty($elements->item(0)->nodeValue) ? 0 : $elements->item(0)->nodeValue * 1;
88
89             $curr = DB_DataObject::factory('core_curr_rate');
90             
91             $curr->curr = $c;
92             
93             $o = false;
94             
95             if($curr->find(true)){
96                 $o = clone($curr);
97             }
98             
99             $curr->setFrom(array(
100                 'rate'  => $rate,
101                 'from'  => date('Y-m-d H:i:s', strtotime($fromDate)),
102                 'to'    => date('Y-m-d H:i:s', strtotime($toDate))
103             ));
104
105             (empty($o)) ? $curr->insert() : $curr->update($o);
106             
107         }
108         
109         
110         
111     }
112     
113     function curl($url, $request = array(), $method = 'GET') 
114     {
115          
116         if(is_array($request)){
117             $request = http_build_query($request);
118         }
119         
120         $url = $url . ($method == 'GET' ? "?" . $request : '');  
121         $ch = curl_init($url);
122         
123         if ($method == 'POST') {
124             curl_setopt($ch, CURLOPT_POST, 1);
125             curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
126             curl_setopt($ch, CURLOPT_HTTPHEADER,
127                     array("Content-Type: application/x-www-form-urlencoded", "Content-Length: " . strlen($request)));
128         }
129         
130         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
131         
132         curl_setopt($ch, CURLOPT_HEADER, false);
133         curl_setopt($ch, CURLOPT_VERBOSE, 1);
134         curl_setopt($ch, CURLOPT_TIMEOUT, 30);
135         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
136
137         $response = curl_exec($ch);
138
139         curl_close($ch);
140         
141         return $response;
142     }
143     
144     /*
145     lang:en
146     result:1
147     date1:10/14/14
148     date:10/20/14
149     date_fmt:us
150     exch:USD
151     exch2:
152     expr:EUR
153     expr2:
154     margin_fixed:0
155     format:HTML
156     SUBMIT:Get Table
157     */
158 }