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