-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExchangeRate.php
More file actions
86 lines (44 loc) · 1.6 KB
/
ExchangeRate.php
File metadata and controls
86 lines (44 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class ExchangeRate
{
private $url;
public function __construct()
{
$this->url='http://www.boc.cn/sourcedb/whpj/';
}
public function GetExchangeRate()
{
$finalouput=array();
$ch=curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch,CURLOPT_URL,$this->url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$output=curl_exec($ch);
curl_close($ch);
/** Obtain the TABLE tag on page */
preg_match_all('/<table[.\s\S]*?>[.\s\S]*?<\/table>/',$output,$table,PREG_PATTERN_ORDER);
/** The second table is about foreign exchange ($table[0][1]) */
/** Obtain <tr> from foreign exchange table */
preg_match_all('/<tr>[.\s\S]*?<\/tr>/',$table[0][1],$trs,PREG_PATTERN_ORDER);
for($i=0;$i<COUNT($trs[0]);$i++)
{
$currency=array();
/** Obtain td from tr, each td contains a selling or buying rate of a currency*/
if(preg_match_all('/<td>[.\s\S]*?<\/td>/',$trs[0][$i],$tds,PREG_PATTERN_ORDER))
{
for($j=0;$j<COUNT($tds[0]);$j++)
{
/** obtain the data from td */
preg_match_all("|<[^>]+>(.*)</[^>]+>|U",$tds[0][$j],$out,PREG_PATTERN_ORDER);
$temp=$out[1][0];
array_push($currency,$temp);
}
}
if(COUNT($currency)>0)
{
array_push($finalouput,$currency);
}
}
return $finalouput;
}
}