2 /** @package verysimple::HTTP */
5 * HttpRequest is a utility method for makine HTTP requests
7 * @package verysimple::HTTP
8 * @author VerySimple Inc.
9 * @copyright 1997-2007 VerySimple, Inc.
10 * @license http://www.gnu.org/licenses/lgpl.html LGPL
15 static $METHOD_GET = "GET";
16 static $METHOD_POST = "POST";
17 static $METHOD_PUT = "PUT";
18 static $METHOD_DELETE = "DELETE";
19 static $USER_AGENT = "verysimple::HttpRequest";
20 static $VERIFY_CERT = false;
29 static function RestRequest($endpoint, $method, $params = array())
31 $qs = HttpRequest
::ArrayToQueryString($params);
35 case HttpRequest
::$METHOD_GET:
36 $ch = curl_init($endpoint . ($qs ?
"?" . $qs : ""));
38 case HttpRequest
::$METHOD_POST:
39 $ch = curl_init($endpoint);
40 curl_setopt($ch, CURLOPT_POST
, 1);
41 curl_setopt($ch, CURLOPT_POSTFIELDS
, $qs);
43 case HttpRequest
::$METHOD_PUT:
44 $ch = curl_init($endpoint);
45 // curl_setopt($ch, CURLOPT_PUT, 1); // <- this method requires CURLOPT_INFILE
46 curl_setopt($ch, CURLOPT_CUSTOMREQUEST
, 'PUT');
47 curl_setopt($ch, CURLOPT_POSTFIELDS
, $qs);
49 case HttpRequest
::$METHOD_DELETE:
50 $ch = curl_init($endpoint);
51 curl_setopt($ch, CURLOPT_CUSTOMREQUEST
, "DELETE");
52 curl_setopt($ch, CURLOPT_POSTFIELDS
, $qs);
56 curl_setopt($ch, CURLOPT_HTTPHEADER
, array (
58 )); // Fixes the HTTP/1.1 417 Expectation Failed Bug
60 curl_setopt($ch, CURLOPT_FOLLOWLOCATION
, 1);
61 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, 1);
62 curl_setopt($ch, CURLOPT_VERBOSE
, 0); // <- ENABLE DEBUGGING
63 curl_setopt($ch, CURLOPT_USERAGENT
, HttpRequest
::$USER_AGENT);
64 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER
, HttpRequest
::$VERIFY_CERT);
65 curl_setopt($ch, CURLOPT_NOPROGRESS
, 1);
68 $response = curl_exec($ch);
70 // if error is not empty, then a network error occured
71 $error = curl_error($ch);
82 * Make an HTTP POST request using the best method available on the server
86 * (array of field/value pairs)
88 * bool true to require verification of SSL cert
91 static function Post($url, $data, $verify_cert = false, $timeout = 30)
93 if (function_exists("curl_init")) {
94 return HttpRequest
::CurlPost($url, $data, $verify_cert, $timeout);
96 return HttpRequest
::FilePost($url, $data, $verify_cert, $timeout);
101 * Make an HTTP GET request using the best method available on the server
105 * (array of field/value pairs)
107 * bool true to require verification of SSL cert
110 static function Get($url, $data = "", $verify_cert = false, $timeout = 30)
112 if (function_exists("curl_init")) {
113 return HttpRequest
::CurlPost($url, $data, $verify_cert, $timeout);
115 return HttpRequest
::FilePost($url, $data, $verify_cert, $timeout);
120 * Make an HTTP PUT reequest using the best method available on the server
124 * (array of field/value pairs)
126 * bool true to require verification of SSL cert
128 * int timeout (in seconds)
131 static function Put($url, $data = "", $verify_cert = false, $timeout = 30)
133 if (function_exists("curl_init")) {
134 return HttpRequest
::CurlPut($url, $data, $verify_cert, $timeout);
136 throw new Exception('PUT request is not supported on systems without curl installed');
141 * Make an HTTP GET request using file_get_contents
145 * (array of field/value pairs)
147 * bool true to require verification of SSL cert
150 static function FileGet($url, $data = "", $verify_cert = false, $timeout = 30)
152 $qs = HttpRequest
::ArrayToQueryString($data);
153 $full_url = $url . ($qs ?
"?" . $qs : "");
154 return file_get_contents($full_url);
158 * Make an HTTP POST request using file_get_contents
162 * (array of field/value pairs)
164 * bool true to require verification of SSL cert
167 static function FilePost($url, $data = "", $verify_cert = false, $timeout = 30)
169 $qs = HttpRequest
::ArrayToQueryString($data);
170 $url = $url . ($qs ?
"?" . $qs : "");
172 $show_headers = false;
173 $url = parse_url($url);
175 if (! isset($url ['port'])) {
176 if ($url ['scheme'] == 'http') {
178 } elseif ($url ['scheme'] == 'https') {
183 $url ['query'] = isset($url ['query']) ?
$url ['query'] : '';
185 $url ['protocol'] = $url ['scheme'] . '://';
188 $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'];
189 $fp = fsockopen($url ['host'], $url ['port'], $errno, $errstr, 30);
191 fputs($fp, $headers);
193 while (! feof($fp)) {
194 $result .= fgets($fp, 128);
198 if (! $show_headers) {
200 $match = preg_split("/\r\n\r\n/s", $result, 2);
201 $result = $match [1];
209 * Make an HTTP GET request using CURL
212 * @param variant $data
213 * querystring or array of field/value pairs
215 * bool true to require verification of SSL cert
218 static function CurlGet($url, $data = "", $verify_cert = false, $timeout = 30)
220 return HttpRequest
::CurlRequest("GET", $url, $data, $verify_cert, $timeout);
224 * Make an HTTP POST request using CURL
227 * @param variant $data
228 * querystring or array of field/value pairs
230 * bool true to require verification of SSL cert
233 static function CurlPost($url, $data, $verify_cert = false, $timeout = 30)
235 return HttpRequest
::CurlRequest("POST", $url, $data, $verify_cert, $timeout);
239 * Make an HTTP PUT request using CURL
242 * @param variant $data
243 * querystring or array of field/value pairs
245 * bool true to require verification of SSL cert
248 static function CurlPut($url, $data, $verify_cert = false, $timeout = 30)
250 return HttpRequest
::CurlRequest("PUT", $url, $data, $verify_cert, $timeout);
254 * Make an HTTP request using CURL
257 * string "POST" or "GET"
259 * @param variant $data
260 * querystring or array of field/value pairs
262 * bool true to require verification of SSL cert
265 static function CurlRequest($method, $url, $data, $verify_cert = false, $timeout = 30)
267 // if the data provided is in array format, convert it to a querystring
268 $qs = HttpRequest
::ArrayToQueryString($data);
270 $agent = "verysimple::HttpRequest";
272 // $header[] = "Accept: text/vnd.wap.wml,*.*";
276 if ($method == "POST") {
277 $ch = curl_init($url);
278 curl_setopt($ch, CURLOPT_POST
, 1);
279 curl_setopt($ch, CURLOPT_POSTFIELDS
, $qs);
280 } elseif ($method == 'PUT') {
281 $ch = curl_init($url);
282 curl_setopt($ch, CURLOPT_PUT
, true);
285 // with a PUT request the body must be written to a file stream
286 $fp = fopen('php://temp/maxmemory:256000', 'w');
288 throw new Exception('Unable to write to php://temp for PUT request');
294 // if the PUT request contains JSON data then add the content type header
295 if (json_encode($data)) {
296 curl_setopt($ch, CURLOPT_HTTPHEADER
, array (
297 "Content-Type: application/json"
301 curl_setopt($ch, CURLOPT_INFILE
, $fp);
302 curl_setopt($ch, CURLOPT_INFILESIZE
, strlen($data));
303 curl_setopt($ch, CURLOPT_BINARYTRANSFER
, true);
306 $full_url = $url . ($qs ?
"?" . $qs : "");
307 $ch = curl_init($full_url);
310 curl_setopt($ch, CURLOPT_HTTPHEADER
, array (
312 )); // Fixes the HTTP/1.1 417 Expectation Failed Bug
314 curl_setopt($ch, CURLOPT_FOLLOWLOCATION
, 1);
315 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, 1);
316 curl_setopt($ch, CURLOPT_VERBOSE
, 0); // ########## debug
317 curl_setopt($ch, CURLOPT_USERAGENT
, $agent);
318 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER
, $verify_cert);
319 curl_setopt($ch, CURLOPT_NOPROGRESS
, 1);
320 // curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
321 // curl_setopt($ch, CURLOPT_COOKIEJAR, "curl_cookie");
322 // curl_setopt($ch, CURLOPT_COOKIEFILE, "curl_cookie");
323 curl_setopt($ch, CURLOPT_TIMEOUT
, $timeout);
325 $tmp = curl_exec($ch);
326 $error = curl_error($ch);
329 @fclose
($fp); // if a PUT request had body data, close the file stream
342 * Converts an array into a URL querystring
345 * array key/value pairs
348 static function ArrayToQueryString($arr)
352 if (is_array($arr)) {
353 // convert the data array into a url querystring
356 foreach (array_keys($arr) as $key) {
357 $qs .= $delim . $key . "=" . $arr [$key];