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     
17     function getAuth() 
18     {
19         $ff = HTML_FlexyFramework::get();
20         if (!empty($ff->cli)) {
21             $this->cli = true;
22             return true;
23         }
24         
25         die("NOT ALLOWED");
26     }
27     
28     function get()
29     {
30         echo "obtain the currency lists... \n";
31         
32         $currency = array();
33         
34         $response = $this->curl($this->actionUrl, array(), 'GET');
35         
36         $doc = new DOMDocument();
37         $doc->loadHTML($response);
38         
39         $xpath = new DOMXpath($doc);
40         
41         $elements = $xpath->query("//select[@name='exch']/option");
42         
43         foreach($elements as $el) {
44             $currency[] = $el->getAttribute('value');
45         }
46         
47         if(empty($currency)){
48             die('no any currency');
49         }
50         
51 //        $params = array(
52 //            'lang' => 'en',
53 //            'result' => 1,
54 //            'date1' => '10/14/14',
55 //            'date'=> '10/20/14',
56 //            'date_fmt' => 'us',
57 //            'exch' => 'CNY',
58 //            'expr' => 'USD',
59 //            'margin_fixed' => 0,
60 //            'format'=> 'HTML'
61 //        );
62 //        
63 //        $response = $this->curl($this->actionUrl, $params, 'POST');
64 //        
65 //        file_put_contents('/tmp/test.html', $response);
66         
67     }
68     
69     function curl($url, $request = array(), $method = 'GET') 
70     {
71          
72         if(is_array($request)){
73             $request = http_build_query($request);
74         }
75         
76         $url = $url . ($method == 'GET' ? "?" . $request : '');  
77         $ch = curl_init($url);
78         
79         if ($method == 'POST') {
80             curl_setopt($ch, CURLOPT_POST, 1);
81             curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
82             curl_setopt($ch, CURLOPT_HTTPHEADER,
83                     array("Content-Type: application/x-www-form-urlencoded", "Content-Length: " . strlen($request)));
84         }
85         
86         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
87         
88         curl_setopt($ch, CURLOPT_HEADER, false);
89         curl_setopt($ch, CURLOPT_VERBOSE, 1);
90         curl_setopt($ch, CURLOPT_TIMEOUT, 30);
91         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
92
93         $response = curl_exec($ch);
94
95         curl_close($ch);
96         
97         return $response;
98     }
99     
100     /*
101     lang:en
102     result:1
103     date1:10/14/14
104     date:10/20/14
105     date_fmt:us
106     exch:USD
107     exch2:
108     expr:EUR
109     expr2:
110     margin_fixed:0
111     format:HTML
112     SUBMIT:Get Table
113     */
114 }