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