-
Notifications
You must be signed in to change notification settings - Fork 207
/
HttpRequest.php
346 lines (304 loc) · 9.6 KB
/
HttpRequest.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
/** @package verysimple::HTTP */
/**
* HttpRequest is a utility method for makine HTTP requests
*
* @package verysimple::HTTP
* @author VerySimple Inc.
* @copyright 1997-2007 VerySimple, Inc.
* @license http://www.gnu.org/licenses/lgpl.html LGPL
* @version 2.0
*/
class HttpRequest
{
static $METHOD_GET = "GET";
static $METHOD_POST = "POST";
static $METHOD_PUT = "PUT";
static $METHOD_DELETE = "DELETE";
static $USER_AGENT = "verysimple::HttpRequest";
static $VERIFY_CERT = false;
/**
*
* @param $method
* @param $params
*/
static function RestRequest($endpoint, $method, $params = Array())
{
$qs = HttpRequest::ArrayToQueryString($params);
$ch = null;
switch ($method)
{
case HttpRequest::$METHOD_GET:
$ch = curl_init($endpoint . ($qs ? "?" . $qs : ""));
break;
case HttpRequest::$METHOD_POST:
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $qs);
break;
case HttpRequest::$METHOD_PUT:
$ch = curl_init($endpoint);
// curl_setopt($ch, CURLOPT_PUT, 1); // <- this method requires CURLOPT_INFILE
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $qs);
break;
case HttpRequest::$METHOD_DELETE:
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_POSTFIELDS, $qs);
break;
}
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect: ") ); //Fixes the HTTP/1.1 417 Expectation Failed Bug
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0); // <- ENABLE DEBUGGING
curl_setopt($ch, CURLOPT_USERAGENT, HttpRequest::$USER_AGENT);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, HttpRequest::$VERIFY_CERT);
curl_setopt($ch, CURLOPT_NOPROGRESS, 1);
// make the request
$response = curl_exec ($ch);
// if error is not empty, then a network error occured
$error = curl_error($ch);
if ($error) {$response .= $error;}
curl_close ($ch);
return $response;
}
/**
* Make an HTTP POST request using the best method available on the server
*
* @param string $url
* @param array $data (array of field/value pairs)
* @param bool true to require verification of SSL cert
* @return string
*/
static function Post($url, $data, $verify_cert = false, $timeout = 30)
{
if (function_exists("curl_init"))
{
return HttpRequest::CurlPost($url, $data, $verify_cert, $timeout);
}
else
{
return HttpRequest::FilePost($url, $data, $verify_cert, $timeout);
}
}
/**
* Make an HTTP GET request using the best method available on the server
*
* @param string $url
* @param array $data (array of field/value pairs)
* @param bool true to require verification of SSL cert
* @return string
*/
static function Get($url, $data = "", $verify_cert = false, $timeout = 30)
{
if (function_exists("curl_init"))
{
return HttpRequest::CurlPost($url, $data, $verify_cert, $timeout);
}
else
{
return HttpRequest::FilePost($url, $data, $verify_cert, $timeout);
}
}
/**
* Make an HTTP PUT reequest using the best method available on the server
*
* @param string $url
* @param array $data (array of field/value pairs)
* @param bool true to require verification of SSL cert
* @param int timeout (in seconds)
* @return string
*/
static function Put($url, $data = "", $verify_cert = false, $timeout = 30)
{
if (function_exists("curl_init"))
{
return HttpRequest::CurlPut($url, $data, $verify_cert, $timeout);
}
else
{
throw new Exception('PUT request is not supported on systems without curl installed');
}
}
/**
* Make an HTTP GET request using file_get_contents
*
* @param string $url
* @param array $data (array of field/value pairs)
* @param bool true to require verification of SSL cert
* @return string
*/
static function FileGet($url, $data = "", $verify_cert = false, $timeout = 30)
{
$qs = HttpRequest::ArrayToQueryString($data);
$full_url = $url . ($qs ? "?" . $qs : "");
return file_get_contents( $full_url );
}
/**
* Make an HTTP POST request using file_get_contents
*
* @param string $url
* @param array $data (array of field/value pairs)
* @param bool true to require verification of SSL cert
* @return string
*/
static function FilePost($url, $data = "", $verify_cert = false, $timeout = 30)
{
$qs = HttpRequest::ArrayToQueryString($data);
$url = $url . ($qs ? "?" . $qs : "");
$show_headers = false;
$url = parse_url($url);
if (!isset($url['port'])) {
if ($url['scheme'] == 'http') { $url['port']=80; }
elseif ($url['scheme'] == 'https') { $url['port']=443; }
}
$url['query']=isset($url['query'])?$url['query']:'';
$url['protocol']=$url['scheme'].'://';
$eol="\r\n";
$headers = "POST ".$url['protocol'].$url['host'].$url['path']." HTTP/1.0".$eol.
"Host: ".$url['host'].$eol.
"Referer: ".$url['protocol'].$url['host'].$url['path'].$eol.
"Content-Type: application/x-www-form-urlencoded".$eol.
"Content-Length: ".strlen($url['query']).$eol.
$eol.$url['query'];
$fp = fsockopen($url['host'], $url['port'], $errno, $errstr, 30);
if($fp)
{
fputs($fp, $headers);
$result = '';
while(!feof($fp)) { $result .= fgets($fp, 128); }
fclose($fp);
if (!$show_headers)
{
//removes headers
$match = preg_split("/\r\n\r\n/s",$result,2);
$result = $match[1];
}
return $result;
}
}
/**
* Make an HTTP GET request using CURL
*
* @param string $url
* @param variant $data querystring or array of field/value pairs
* @param bool true to require verification of SSL cert
* @return string
*/
static function CurlGet($url, $data = "", $verify_cert = false, $timeout = 30)
{
return HttpRequest::CurlRequest("GET",$url, $data, $verify_cert, $timeout);
}
/**
* Make an HTTP POST request using CURL
*
* @param string $url
* @param variant $data querystring or array of field/value pairs
* @param bool true to require verification of SSL cert
* @return string
*/
static function CurlPost($url, $data, $verify_cert = false, $timeout = 30)
{
return HttpRequest::CurlRequest("POST",$url, $data, $verify_cert, $timeout);
}
/**
* Make an HTTP PUT request using CURL
*
* @param string $url
* @param variant $data querystring or array of field/value pairs
* @param bool true to require verification of SSL cert
* @return string
*/
static function CurlPut($url, $data, $verify_cert = false, $timeout = 30)
{
return HttpRequest::CurlRequest("PUT",$url, $data, $verify_cert, $timeout);
}
/**
* Make an HTTP request using CURL
*
* @param string "POST" or "GET"
* @param string $url
* @param variant $data querystring or array of field/value pairs
* @param bool true to require verification of SSL cert
* @return string
*/
static function CurlRequest($method, $url, $data, $verify_cert = false, $timeout = 30)
{
// if the data provided is in array format, convert it to a querystring
$qs = HttpRequest::ArrayToQueryString($data);
$agent = "verysimple::HttpRequest";
// $header[] = "Accept: text/vnd.wap.wml,*.*";
$fp = null;
if ($method == "POST")
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $qs);
}
elseif ($method == 'PUT')
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_PUT, true);
if ($data)
{
// with a PUT request the body must be written to a file stream
$fp = fopen('php://temp/maxmemory:256000', 'w');
if (!$fp) {
throw new Exception('Unable to write to php://temp for PUT request');
}
fwrite($fp, $data);
fseek($fp, 0);
// if the PUT request contains JSON data then add the content type header
if (json_encode($data)) curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json") );
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($data));
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
}
}
else
{
$full_url = $url . ($qs ? "?" . $qs : "");
$ch = curl_init($full_url);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect: ") ); //Fixes the HTTP/1.1 417 Expectation Failed Bug
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0); ########### debug
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verify_cert);
curl_setopt($ch, CURLOPT_NOPROGRESS, 1);
// curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
// curl_setopt($ch, CURLOPT_COOKIEJAR, "curl_cookie");
// curl_setopt($ch, CURLOPT_COOKIEFILE, "curl_cookie");
curl_setopt($ch, CURLOPT_TIMEOUT,$timeout);
$tmp = curl_exec ($ch);
$error = curl_error($ch);
if ($fp) @fclose($fp); // if a PUT request had body data, close the file stream
if ($error != "") {$tmp .= $error;}
curl_close ($ch);
return $tmp;
}
/**
* Converts an array into a URL querystring
* @param array key/value pairs
* @return string
*/
static function ArrayToQueryString($arr)
{
$qs = $arr;
if (is_array($arr))
{
// convert the data array into a url querystring
$qs = "";
$delim = "";
foreach (array_keys($arr) as $key)
{
$qs .= $delim . $key ."=" . $arr[$key];
$delim = "&";
}
}
return $qs;
}
}
?>