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