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