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