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     function curl($url, $request = array(), $method = 'GET') 
112     {
113          
114         if(is_array($request)){
115             $request = http_build_query($request);
116         }
117         
118         $url = $url . ($method == 'GET' ? "?" . $request : '');  
119         $ch = curl_init($url);
120         
121         if ($method == 'POST') {
122             curl_setopt($ch, CURLOPT_POST, 1);
123             curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
124             curl_setopt($ch, CURLOPT_HTTPHEADER,
125                     array("Content-Type: application/x-www-form-urlencoded", "Content-Length: " . strlen($request)));
126         }
127         
128         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
129         
130         curl_setopt($ch, CURLOPT_HEADER, false);
131         curl_setopt($ch, CURLOPT_VERBOSE, 1);
132         curl_setopt($ch, CURLOPT_TIMEOUT, 30);
133         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
134
135         $response = curl_exec($ch);
136
137         curl_close($ch);
138         
139         return $response;
140     }
141     
142     /*
143     lang:en
144     result:1
145     date1:10/14/14
146     date:10/20/14
147     date_fmt:us
148     exch:USD
149     exch2:
150     expr:EUR
151     expr2:
152     margin_fixed:0
153     format:HTML
154     SUBMIT:Get Table
155     */
156 }