-
Notifications
You must be signed in to change notification settings - Fork 0
/
Curl.php
272 lines (242 loc) · 5.72 KB
/
Curl.php
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
<?php
namespace SnooPHP\Curl;
/**
* Provides a friendly interface to perform HTTP requests using native php curl library
*
* @see Get
* @see Post
* @see Put
* @see Delete
*
* @author sneppy
*/
class Curl
{
/**
* @var resource $curl underlying curl resource
*/
protected $curl;
/**
* @var string $url session url
*/
protected $url;
/**
* @var string $lastResult result of last execution
*/
protected $lastResult = null;
/**
* @var string $lastResultType response Content-Type value
*/
protected $lastResultType = "text/plain";
/**
* @var array $lastResultHeader set of headers from the last result
*/
protected $lastHeader = [];
/**
* @var array $info array with information about the session
*/
protected $info = null;
/**
* Create a new Curl session
*
* @param string $url session url
* @param array $options set of options
* @param array $headers list of http headers
* @param bool $initOnly if true the session won't be executed
*/
public function __construct($url, array $options = [], array $headers = [], $initOnly = true)
{
// Init curl session
$this->curl = curl_init($this->url = $url);
// Set options
$this->option($options);
$this->option([CURLOPT_HEADERFUNCTION => [&$this, "parseHeader"]]);
// Set headers
$headerList = [];
foreach ($headers as $header => $val) $headerList[] = $header.": ".$val;
$this->option([CURLOPT_HTTPHEADER => $headerList]);
// Execute session
if (!$initOnly) $this->exec();
}
/**
* Execute and close session
*
* @param bool $keepAlive if true the session will not be closed
*
* @return bool false if execution failed, true otherwise
*/
public function exec($keepAlive = false)
{
$this->lastResult = curl_exec($this->curl);
$this->info = curl_getinfo($this->curl);
// Close session
if (!$keepAlive) curl_close($this->curl);
return $this->lastResult !== false;
}
/**
* Get curl response body
*
* @param bool $decodeJson if true and return type is json, decode content
*
* @return null|bool|string
*/
public function content($decodeJson = false)
{
return $decodeJson && preg_match("~^application/json.*~", $this->lastResultType) && $this->lastResult ?
from_json($this->lastResult) :
$this->lastResult;
}
/**
* Get curl reponse content type
*
* @return string
*/
public function contentType()
{
return $this->lastResultType;
}
/**
* Get curl response code
*
* @return int
*/
public function code()
{
return $this->info ?
$this->info["http_code"] :
curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
}
/**
* Return true if response is success
*
* @return bool
*/
public function success()
{
return $this->code() < 400;
}
/**
* Get session info
*
* @param string $name if set return associated information
*
* @return array|string
*/
public function info($name = null)
{
if ($name)
{
return $this->info ?
$this->info[$name] :
curl_getinfo($this->curl, "CURLINFO".strtoupper($name));
}
else
{
return $this->info ?: curl_getinfo($this->curl);
}
}
/**
* Set or get session url
*
* @param string|null $url if set update session url
*
* @return string
*/
public function url($url = null)
{
if ($url)
{
$this->url = $url;
$this->option([CURLOPT_URL => $url]);
}
return $this->url;
}
/**
* Set option
*
* @param array $options associative array
*/
public function option(array $options = [])
{
curl_setopt_array($this->curl, $options);
}
/**
* Return last error as string
*
* @return string
*/
public function error()
{
return curl_error($this->curl);
}
/**
* Return last error code
*
* @return int|null
*/
public function errorCode()
{
return curl_errno($this->curl);
}
/**
* Parse header line
*
* @param resource $curl curl resource
* @param string $header header line
*
* @return int
*/
protected function parseHeader($curl, $header)
{
if (preg_match("/^([^:\s]+)\:\s+(.*)$/", $header, $matches))
{
// Add to header list
$matches[2] = trim($matches[2]);
$this->lastHeader[$matches[1]] = $matches[2];
// Set result type
$this->lastResultType = $matches[1] === "Content-Type" ? $matches[2] : $this->lastResultType;
}
return strlen($header);
}
/**
* Create appropriate session
*
* @param string $method method string
* @param string $url transfer url
* @param string|array $data data to post [default: ""]
* @param array $headers set of additional headers [default: []]
* @param array $options set of additional options [default: []]
* @param bool $initOnly if true don't send request on creation [default: false]
*
* @return Get|Post|Put|Delete|null null if $method doesn't match any available method
*/
public static function create($method, $url, $data = "", array $headers = [], array $options = [], $initOnly = false)
{
// Null if method is not valid
$curl = null;
// Compare method
if (!strcasecmp($method, "GET"))
$curl = new static($url, $options + [
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_RETURNTRANSFER => true
], $headers, $initOnly);
else if (!strcasecmp($method, "POST"))
$curl = new static($url, $options + [
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => true
], $headers, $initOnly);
else if (!strcasecmp($method, "PUT"))
$curl = new static($url, $options + [
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => true
], $headers, $initOnly);
else if (!strcasecmp($method, "DELETE"))
$curl = new static($url, $options + [
CURLOPT_CUSTOMREQUEST => "DELETE",
CURLOPT_RETURNTRANSFER => true
], $headers, $initOnly);
return $curl;
}
}