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 |
|
if (gettype($jsonCache) == "array") { |
46
|
|
|
$jsonCache = $jsonCache[0]; |
47
|
|
|
} |
48
|
9 |
|
return json_decode($jsonCache, true); |
49
|
|
|
} else { |
50
|
|
|
return json_decode($this->singleFetch($link), true); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function singleFetch(string $link) |
55
|
|
|
{ |
56
|
|
|
$curl = curl_init(); |
57
|
|
|
|
58
|
|
|
if ($curl == false) { |
59
|
|
|
return ""; |
60
|
|
|
} |
61
|
|
|
curl_setopt($curl, CURLOPT_URL, $link); |
62
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
63
|
|
|
$data = curl_exec($curl); |
64
|
|
|
curl_close($curl); |
65
|
|
|
return $data; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Fetch multidata. |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
|
74
|
3 |
|
public function getMultiData(array $links) |
75
|
|
|
{ |
76
|
3 |
|
$outputArr = array(); |
77
|
3 |
|
if ($this->testMode == true) { |
78
|
3 |
|
$forceRefresh = false; |
79
|
3 |
|
$refresh = 60 * 60 * 13; |
80
|
3 |
|
$url = $links[1]; |
81
|
3 |
|
$tempUrl = preg_replace("/[,][0-9]*[?]/u", "", $url); |
82
|
3 |
|
$file = preg_replace("/[^[:alnum:][:space:]]/u", '', $tempUrl); |
83
|
3 |
|
$cache = ANAX_INSTALL_PATH . "/test/cache/weather/multi-$file.cache"; |
84
|
3 |
|
if (!is_file(($cache))) { |
85
|
|
|
$handle = fopen($cache, 'wb'); |
86
|
|
|
if ($handle === false) { |
87
|
|
|
return array(); |
88
|
|
|
} |
89
|
|
|
fclose($handle); |
90
|
|
|
$forceRefresh = true; |
91
|
|
|
} |
92
|
|
|
|
93
|
3 |
|
if ($forceRefresh === true || ((time() - filectime($cache)) > ($refresh) || 0 == filesize($cache))) { |
94
|
|
|
$outputArr = $this->fetchMultiData($links); |
95
|
|
|
file_put_contents($cache, json_encode($outputArr)); |
96
|
|
|
} else { |
97
|
3 |
|
$jsonCache = file_get_contents($cache); |
98
|
3 |
|
$jsonCache = json_decode($jsonCache, true)[1]; |
99
|
3 |
|
array_push($outputArr, $jsonCache); |
100
|
|
|
} |
101
|
|
|
} else { |
102
|
|
|
$outputArr = $this->fetchMultiData($links); |
103
|
|
|
} |
104
|
3 |
|
return $outputArr; |
105
|
|
|
} |
106
|
|
|
/* |
107
|
|
|
* Fetch multi data from API |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
private function fetchMultiData(array $links) |
111
|
|
|
{ |
112
|
|
|
$multiCurl = array(); |
113
|
|
|
$outputArr = array(); |
114
|
|
|
//create the multiple cURL handle |
115
|
|
|
$multiHandler = curl_multi_init(); |
116
|
|
|
foreach ($links as $link) { |
117
|
|
|
$curlHandler = curl_init(); |
118
|
|
|
if ($curlHandler === false) { |
119
|
|
|
return array(); |
120
|
|
|
} |
121
|
|
|
// set URL and other appropriate options |
122
|
|
|
curl_setopt($curlHandler, CURLOPT_URL, $link); |
123
|
|
|
curl_setopt($curlHandler, CURLOPT_HEADER, 0); |
124
|
|
|
curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, true); |
125
|
|
|
//add the two handles |
126
|
|
|
curl_multi_add_handle($multiHandler, $curlHandler); |
127
|
|
|
array_push($multiCurl, $curlHandler); |
128
|
|
|
} |
129
|
|
|
//execute the multi handle |
130
|
|
|
$active = null; |
131
|
|
|
do { |
132
|
|
|
curl_multi_exec($multiHandler, $active); |
133
|
|
|
} while ($active); |
134
|
|
|
|
135
|
|
|
//close the handles |
136
|
|
|
foreach ($multiCurl as $handler) { |
137
|
|
|
curl_multi_remove_handle($multiHandler, $handler); |
138
|
|
|
} |
139
|
|
|
curl_multi_close($multiHandler); |
140
|
|
|
foreach ($multiCurl as $handler) { |
141
|
|
|
$data = curl_multi_getcontent($handler); |
142
|
|
|
array_push($outputArr, json_decode($data, true)); |
143
|
|
|
} |
144
|
|
|
return $outputArr; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Set test mode. |
149
|
|
|
* |
150
|
|
|
*/ |
151
|
|
|
|
152
|
20 |
|
public function setTestMode(bool $mode) |
153
|
|
|
{ |
154
|
20 |
|
$this->testMode = $mode; |
155
|
20 |
|
} |
156
|
|
|
} |
157
|
|
|
|