-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCurl.php
More file actions
284 lines (249 loc) · 7.22 KB
/
Curl.php
File metadata and controls
284 lines (249 loc) · 7.22 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
/**
* Arikaim
*
* @link http://www.arikaim.com
* @copyright Copyright (c) Konstantin Atanasov <info@arikaim.com>
* @license http://www.arikaim.com/license
*
*/
namespace Arikaim\Core\Utils;
/**
* Curl wrapper
*/
class Curl
{
const TIMEOUT = 60;
/**
* User agent
*
* @var string|null
*/
public static $userAgent = null;
/**
* Verbose option
*
* @var boolean
*/
public static $verbose = false;
/**
* Response code
*
* @var mixed
*/
private static $responseCode = null;
/**
* Return true if php curl extension is installed
*
* @return boolean
*/
public static function isInsatlled(): bool
{
return \extension_loaded('curl');
}
/**
* Create curl
*
* @param string $url
* @param integer $timeout
* @param boolean $returnTransfer
* @return object|null
*/
private static function create(string $url, $timeout = 30, bool $returnTransfer = true)
{
if (Self::isInsatlled() == false) {
return null;
}
$curl = \curl_init();
\curl_setopt($curl,CURLOPT_URL,$url);
\curl_setopt($curl,CURLOPT_VERBOSE,Self::$verbose);
\curl_setopt($curl,CURLOPT_RETURNTRANSFER,$returnTransfer);
\curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,$timeout);
if (empty(Self::$userAgent) == false) {
\curl_setopt($curl,CURLOPT_USERAGENT,Self::$userAgent);
}
return $curl;
}
/**
* Fet response code
*
* @return mixed
*/
public static function getResponseCode()
{
return Self::$responseCode;
}
/**
* Run curl command
*
* @param object $curl
* @return mixed
*/
private static function exec($curl)
{
Self::$responseCode = null;
$response = \curl_exec($curl);
$error = ($response === false) ? \curl_error($curl) : null;
Self::$responseCode = \curl_getinfo($curl,CURLINFO_HTTP_CODE);
\curl_close($curl);
return (empty($error) == true) ? $response : $error;
}
/**
* Run curl request
*
* @param string $url
* @param string $method
* @param array|string|null $data
* @param array $headers
* @param integer $timeout
* @return mixed
*/
public static function request(string $url, string $method, $data = null, ?array $headers = null, int $timeout = Self::TIMEOUT)
{
$curl = Self::create($url,$timeout);
if (empty($curl) == true) {
return false;
}
\curl_setopt($curl,CURLOPT_CUSTOMREQUEST,$method);
if (empty($data) == false) {
$data = (\is_array($data) == true) ? json_encode($data) : $data;
$curl = Self::setData($curl,$data,$method);
$headers = (\is_array($headers) == true) ? $headers : [];
$headers = \array_merge($headers,[
'Content-Length: ' . \strlen($data)
]);
}
if (\is_array($headers) == true) {
\curl_setopt($curl,CURLOPT_HTTPHEADER,$headers);
}
return Self::exec($curl);
}
/**
* Set post data
*
* @param object $curl
* @param mixed $data
* @param string $method
* @return object
*/
public static function setData($curl, $data, string $method = 'POST')
{
if (empty($data) == true) {
return $curl;
}
if ($method == 'POST' || $method == 'PUT') {
\curl_setopt($curl,CURLOPT_POST,1);
\curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
}
return $curl;
}
/**
* Run POST request
*
* @param string $url
* @param array|string|null $data
* @param array|null $headers
* @param integer $timeout
* @return mixed
*/
public static function post(string $url, $data = null, ?array $headers = null, int $timeout = Self::TIMEOUT)
{
return Self::request($url,'POST',$data,$headers,$timeout);
}
/**
* Run GET request
*
* @param string $url
* @param array|null $data
* @param array|null $headers
* @param integer $timeout
* @return mixed
*/
public static function get(string $url, ?array $data = null, ?array $headers = null, int $timeout = Self::TIMEOUT)
{
return Self::request($url,'GET',$data,$headers,$timeout);
}
/**
* Run DELETE request.
*
* @param string $url
* @param array|null $data
* @param array|null $headers
* @param integer $timeout
* @return mixed
*/
public static function delete(string $url, ?array $data = null, ?array $headers = null, $timeout = Self::TIMEOUT)
{
return Self::request($url,'DELETE',$data,$headers,$timeout);
}
/**
* Run PUT request
*
* @param string $url
* @param array|null $data
* @param array|null $headers
* @param integer $timeout
* @return mixed
*/
public static function put(string $url, ?array $data = null, ?array $headers = null, int $timeout = Self::TIMEOUT)
{
return Self::request($url,'PUT',$data,$headers,$timeout);
}
/**
* Download file
*
* @param string $url
* @param string $fileName
* @param string|null $method
* @param array|null $headers
* @return boolean
*/
public static function downloadFile(string $url, string $fileName, ?string $method = null, ?array $headers = null): bool
{
$file = \fopen($fileName,'w+');
$curl = Self::create($url);
$curl = Self::downloadFileInit($curl,$method,$headers);
\curl_setopt($curl,CURLOPT_FILE,$file);
$result = Self::exec($curl,60);
\fclose($file);
return ($result !== false);
}
/**
* Get file content
*
* @param string $url
* @param string|null $method
* @param array|null $headers
* @return mixed
*/
public static function getFileContent(string $url, ?string $method = null, ?array $headers = null)
{
$curl = Self::create($url);
$curl = Self::downloadFileInit($curl,$method,$headers);
return Self::exec($curl,60);
}
/**
* Init curl for file download
*
* @param object $curl
* @param string|null $method
* @param array|null $headers
* @return object
*/
public static function downloadFileInit($curl, ?string $method = null, ?array $headers = null)
{
$method = $method ?? 'GET';
\curl_setopt($curl,CURLOPT_VERBOSE,0);
\curl_setopt($curl,CURLOPT_BINARYTRANSFER,true);
\curl_setopt($curl,CURLOPT_FOLLOWLOCATION,true);
\curl_setopt($curl,CURLOPT_AUTOREFERER,false);
\curl_setopt($curl,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
\curl_setopt($curl,CURLOPT_CUSTOMREQUEST,$method);
\curl_setopt($curl,CURLOPT_HEADER,0);
\curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
if (\is_array($headers) == true) {
\curl_setopt($curl,CURLOPT_HTTPHEADER,$headers);
}
return $curl;
}
}