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         
59         
60         
61 //        $params = array(
62 //            'lang' => 'en',
63 //            'result' => 1,
64 //            'date1' => '10/14/14',
65 //            'date'=> '10/20/14',
66 //            'date_fmt' => 'us',
67 //            'exch' => 'CNY',
68 //            'expr' => 'USD',
69 //            'margin_fixed' => 0,
70 //            'format'=> 'HTML'
71 //        );
72 //        
73 //        $response = $this->curl($this->actionUrl, $params, 'POST');
74 //        
75 //        file_put_contents('/tmp/test.html', $response);
76         
77     }
78     
79     function curl($url, $request = array(), $method = 'GET') 
80     {
81          
82         if(is_array($request)){
83             $request = http_build_query($request);
84         }
85         
86         $url = $url . ($method == 'GET' ? "?" . $request : '');  
87         $ch = curl_init($url);
88         
89         if ($method == 'POST') {
90             curl_setopt($ch, CURLOPT_POST, 1);
91             curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
92             curl_setopt($ch, CURLOPT_HTTPHEADER,
93                     array("Content-Type: application/x-www-form-urlencoded", "Content-Length: " . strlen($request)));
94         }
95         
96         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
97         
98         curl_setopt($ch, CURLOPT_HEADER, false);
99         curl_setopt($ch, CURLOPT_VERBOSE, 1);
100         curl_setopt($ch, CURLOPT_TIMEOUT, 30);
101         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
102
103         $response = curl_exec($ch);
104
105         curl_close($ch);
106         
107         return $response;
108     }
109     
110     /*
111     lang:en
112     result:1
113     date1:10/14/14
114     date:10/20/14
115     date_fmt:us
116     exch:USD
117     exch2:
118     expr:EUR
119     expr2:
120     margin_fixed:0
121     format:HTML
122     SUBMIT:Get Table
123     */
124 }