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