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