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