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         print_R(date('m/d/y'));exit;
34         echo "obtain the currency lists... \n";
35         
36         $currency = array();
37         
38         $response = $this->curl($this->actionUrl, array(), 'GET');
39         
40         libxml_use_internal_errors (true);
41         
42         $doc = new DOMDocument();
43         $doc->loadHTML($response);
44         
45         libxml_use_internal_errors (false);
46         
47         $xpath = new DOMXpath($doc);
48         
49         $elements = $xpath->query("//select[@name='exch']/option");
50         
51         foreach($elements as $el) {
52             $currency[] = $el->getAttribute('value');
53         }
54         
55         if(empty($currency)){
56             die('no any currency');
57         }
58         
59         foreach ($currency as $c){
60             $params = array(
61                 'lang' => 'en',
62                 'result' => 1,
63                 'date1' => '10/14/14',
64                 'date'=> date('m/d/y'),
65                 'date_fmt' => 'us',
66                 'exch' => 'CNY',
67                 'expr' => 'USD',
68                 'margin_fixed' => 0,
69                 'format'=> 'HTML'
70             );
71         }
72         
73         
74         
75         
76 //        $params = array(
77 //            'lang' => 'en',
78 //            'result' => 1,
79 //            'date1' => '10/14/14',
80 //            'date'=> '10/20/14',
81 //            'date_fmt' => 'us',
82 //            'exch' => 'CNY',
83 //            'expr' => 'USD',
84 //            'margin_fixed' => 0,
85 //            'format'=> 'HTML'
86 //        );
87 //        
88 //        $response = $this->curl($this->actionUrl, $params, 'POST');
89 //        
90 //        file_put_contents('/tmp/test.html', $response);
91         
92     }
93     
94     function curl($url, $request = array(), $method = 'GET') 
95     {
96          
97         if(is_array($request)){
98             $request = http_build_query($request);
99         }
100         
101         $url = $url . ($method == 'GET' ? "?" . $request : '');  
102         $ch = curl_init($url);
103         
104         if ($method == 'POST') {
105             curl_setopt($ch, CURLOPT_POST, 1);
106             curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
107             curl_setopt($ch, CURLOPT_HTTPHEADER,
108                     array("Content-Type: application/x-www-form-urlencoded", "Content-Length: " . strlen($request)));
109         }
110         
111         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
112         
113         curl_setopt($ch, CURLOPT_HEADER, false);
114         curl_setopt($ch, CURLOPT_VERBOSE, 1);
115         curl_setopt($ch, CURLOPT_TIMEOUT, 30);
116         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
117
118         $response = curl_exec($ch);
119
120         curl_close($ch);
121         
122         return $response;
123     }
124     
125     /*
126     lang:en
127     result:1
128     date1:10/14/14
129     date:10/20/14
130     date_fmt:us
131     exch:USD
132     exch2:
133     expr:EUR
134     expr2:
135     margin_fixed:0
136     format:HTML
137     SUBMIT:Get Table
138     */
139 }