|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* CurlModel. |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Anax\CurlModel; |
|
8
|
|
|
|
|
9
|
|
|
use PhpParser\Node\Expr\Cast\Array_; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Showing off a standard class with methods and properties. |
|
13
|
|
|
*/ |
|
14
|
|
|
class CurlModel |
|
15
|
|
|
{ |
|
16
|
|
|
protected $testMode; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Fetch data. |
|
20
|
|
|
* |
|
21
|
|
|
* @return array |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
9 |
|
public function getData(string $link) |
|
25
|
|
|
{ |
|
26
|
9 |
|
if ($this->testMode == true) { |
|
27
|
9 |
|
$file = preg_replace("/[^[:alnum:][:space:]]/u", '', $link); |
|
28
|
9 |
|
$cache = ANAX_INSTALL_PATH . "/test/cache/weather/$file.cahce"; |
|
29
|
9 |
|
$forceRefresh = false; |
|
30
|
9 |
|
$refresh = 60 * 60 * 13; |
|
31
|
9 |
|
if (!is_file(($cache))) { |
|
32
|
|
|
$handle = fopen($cache, 'wb'); |
|
33
|
|
|
if ($handle === false) { |
|
34
|
|
|
return array(); |
|
35
|
|
|
} |
|
36
|
|
|
fclose($handle); |
|
37
|
|
|
$forceRefresh = true; |
|
38
|
|
|
} |
|
39
|
9 |
|
if ($forceRefresh === true || ((time() - filectime($cache)) > ($refresh) || 0 == filesize($cache))) { |
|
40
|
|
|
$jsonCache = $this->singleFetch($link); |
|
41
|
|
|
file_put_contents($cache, $jsonCache); |
|
42
|
|
|
} else { |
|
43
|
9 |
|
$jsonCache = file_get_contents($cache); |
|
44
|
|
|
} |
|
45
|
9 |
|
return json_decode($jsonCache, true); |
|
|
|
|
|
|
46
|
|
|
} else { |
|
47
|
|
|
return json_decode($this->singleFetch($link), true); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function singleFetch(string $link) |
|
52
|
|
|
{ |
|
53
|
|
|
$curl = curl_init(); |
|
54
|
|
|
|
|
55
|
|
|
if ($curl == false) { |
|
56
|
|
|
return array(); |
|
57
|
|
|
} |
|
58
|
|
|
curl_setopt($curl, CURLOPT_URL, $link); |
|
59
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
|
60
|
|
|
$data = curl_exec($curl); |
|
61
|
|
|
curl_close($curl); |
|
62
|
|
|
return $data; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Fetch multidata. |
|
67
|
|
|
* |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
|
|
|
|
71
|
3 |
|
public function getMultiData(array $links) |
|
72
|
|
|
{ |
|
73
|
3 |
|
$outputArr = array(); |
|
74
|
3 |
|
if ($this->testMode == true) { |
|
75
|
3 |
|
$forceRefresh = false; |
|
76
|
3 |
|
$refresh = 60 * 60 * 13; |
|
77
|
3 |
|
$url = $links[1]; |
|
78
|
3 |
|
$tempUrl = preg_replace("/[,][0-9]*[?]/u", "", $url); |
|
79
|
3 |
|
$file = preg_replace("/[^[:alnum:][:space:]]/u", '', $tempUrl); |
|
80
|
3 |
|
$cache = ANAX_INSTALL_PATH . "/test/cache/weather/multi-$file.cache"; |
|
81
|
3 |
|
if (!is_file(($cache))) { |
|
82
|
|
|
$handle = fopen($cache, 'wb'); |
|
83
|
|
|
if ($handle === false) { |
|
84
|
|
|
return array(); |
|
85
|
|
|
} |
|
86
|
|
|
fclose($handle); |
|
87
|
|
|
$forceRefresh = true; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
3 |
|
if ($forceRefresh === true || ((time() - filectime($cache)) > ($refresh) || 0 == filesize($cache))) { |
|
91
|
|
|
$outputArr = $this->fetchMultiData($links); |
|
92
|
|
|
file_put_contents($cache, $outputArr); |
|
93
|
|
|
} else { |
|
94
|
3 |
|
$jsonCache = file_get_contents($cache); |
|
95
|
3 |
|
array_push($outputArr, json_decode($jsonCache[0], true)); |
|
96
|
|
|
} |
|
97
|
|
|
} else { |
|
98
|
|
|
$outputArr = $this->fetchMultiData($links); |
|
99
|
|
|
} |
|
100
|
3 |
|
return $outputArr; |
|
101
|
|
|
} |
|
102
|
|
|
/* |
|
103
|
|
|
* Fetch multi data from API |
|
104
|
|
|
* @return array |
|
105
|
|
|
*/ |
|
106
|
|
|
private function fetchMultiData(array $links) |
|
107
|
|
|
{ |
|
108
|
|
|
$multiCurl = array(); |
|
109
|
|
|
$outputArr = array(); |
|
110
|
|
|
//create the multiple cURL handle |
|
111
|
|
|
$multiHandler = curl_multi_init(); |
|
112
|
|
|
foreach ($links as $link) { |
|
113
|
|
|
$curlHandler = curl_init(); |
|
114
|
|
|
if ($curlHandler === false) { |
|
115
|
|
|
return array(); |
|
116
|
|
|
} |
|
117
|
|
|
// set URL and other appropriate options |
|
118
|
|
|
curl_setopt($curlHandler, CURLOPT_URL, $link); |
|
119
|
|
|
curl_setopt($curlHandler, CURLOPT_HEADER, 0); |
|
120
|
|
|
curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, true); |
|
121
|
|
|
//add the two handles |
|
122
|
|
|
curl_multi_add_handle($multiHandler, $curlHandler); |
|
123
|
|
|
array_push($multiCurl, $curlHandler); |
|
124
|
|
|
} |
|
125
|
|
|
//execute the multi handle |
|
126
|
|
|
$active = null; |
|
127
|
|
|
do { |
|
128
|
|
|
curl_multi_exec($multiHandler, $active); |
|
129
|
|
|
} while ($active); |
|
130
|
|
|
|
|
131
|
|
|
//close the handles |
|
132
|
|
|
foreach ($multiCurl as $handler) { |
|
133
|
|
|
curl_multi_remove_handle($multiHandler, $handler); |
|
134
|
|
|
} |
|
135
|
|
|
curl_multi_close($multiHandler); |
|
136
|
|
|
foreach ($multiCurl as $handler) { |
|
137
|
|
|
$data = curl_multi_getcontent($handler); |
|
138
|
|
|
array_push($outputArr, json_decode($data, true)); |
|
139
|
|
|
} |
|
140
|
|
|
return $outputArr; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Set test mode. |
|
145
|
|
|
* |
|
146
|
|
|
*/ |
|
147
|
|
|
|
|
148
|
20 |
|
public function setTestMode(bool $mode) |
|
149
|
|
|
{ |
|
150
|
20 |
|
$this->testMode = $mode; |
|
151
|
20 |
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|