Highway to PSR2
[openemr.git] / portal / patient / fwk / libs / verysimple / HTTP / HttpRequest.php
blob4345064a318835ffe4bee76e8e8a39d884708134
1 <?php
2 /** @package verysimple::HTTP */
4 /**
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
11 * @version 2.0
13 class HttpRequest
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;
22 /**
24 * @param
25 * $method
26 * @param
27 * $params
29 static function RestRequest($endpoint, $method, $params = array())
31 $qs = HttpRequest::ArrayToQueryString($params);
32 $ch = null;
34 switch ($method) {
35 case HttpRequest::$METHOD_GET:
36 $ch = curl_init($endpoint . ($qs ? "?" . $qs : ""));
37 break;
38 case HttpRequest::$METHOD_POST:
39 $ch = curl_init($endpoint);
40 curl_setopt($ch, CURLOPT_POST, 1);
41 curl_setopt($ch, CURLOPT_POSTFIELDS, $qs);
42 break;
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);
48 break;
49 case HttpRequest::$METHOD_DELETE:
50 $ch = curl_init($endpoint);
51 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
52 curl_setopt($ch, CURLOPT_POSTFIELDS, $qs);
53 break;
56 curl_setopt($ch, CURLOPT_HTTPHEADER, array (
57 "Expect: "
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);
67 // make the request
68 $response = curl_exec($ch);
70 // if error is not empty, then a network error occured
71 $error = curl_error($ch);
72 if ($error) {
73 $response .= $error;
76 curl_close($ch);
78 return $response;
81 /**
82 * Make an HTTP POST request using the best method available on the server
84 * @param string $url
85 * @param array $data
86 * (array of field/value pairs)
87 * @param
88 * bool true to require verification of SSL cert
89 * @return string
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);
95 } else {
96 return HttpRequest::FilePost($url, $data, $verify_cert, $timeout);
101 * Make an HTTP GET request using the best method available on the server
103 * @param string $url
104 * @param array $data
105 * (array of field/value pairs)
106 * @param
107 * bool true to require verification of SSL cert
108 * @return string
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);
114 } else {
115 return HttpRequest::FilePost($url, $data, $verify_cert, $timeout);
120 * Make an HTTP PUT reequest using the best method available on the server
122 * @param string $url
123 * @param array $data
124 * (array of field/value pairs)
125 * @param
126 * bool true to require verification of SSL cert
127 * @param
128 * int timeout (in seconds)
129 * @return string
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);
135 } else {
136 throw new Exception('PUT request is not supported on systems without curl installed');
141 * Make an HTTP GET request using file_get_contents
143 * @param string $url
144 * @param array $data
145 * (array of field/value pairs)
146 * @param
147 * bool true to require verification of SSL cert
148 * @return string
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
160 * @param string $url
161 * @param array $data
162 * (array of field/value pairs)
163 * @param
164 * bool true to require verification of SSL cert
165 * @return string
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') {
177 $url ['port'] = 80;
178 } elseif ($url ['scheme'] == 'https') {
179 $url ['port'] = 443;
183 $url ['query'] = isset($url ['query']) ? $url ['query'] : '';
185 $url ['protocol'] = $url ['scheme'] . '://';
186 $eol = "\r\n";
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);
190 if ($fp) {
191 fputs($fp, $headers);
192 $result = '';
193 while (! feof($fp)) {
194 $result .= fgets($fp, 128);
197 fclose($fp);
198 if (! $show_headers) {
199 // removes headers
200 $match = preg_split("/\r\n\r\n/s", $result, 2);
201 $result = $match [1];
204 return $result;
209 * Make an HTTP GET request using CURL
211 * @param string $url
212 * @param variant $data
213 * querystring or array of field/value pairs
214 * @param
215 * bool true to require verification of SSL cert
216 * @return string
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
226 * @param string $url
227 * @param variant $data
228 * querystring or array of field/value pairs
229 * @param
230 * bool true to require verification of SSL cert
231 * @return string
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
241 * @param string $url
242 * @param variant $data
243 * querystring or array of field/value pairs
244 * @param
245 * bool true to require verification of SSL cert
246 * @return string
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
256 * @param
257 * string "POST" or "GET"
258 * @param string $url
259 * @param variant $data
260 * querystring or array of field/value pairs
261 * @param
262 * bool true to require verification of SSL cert
263 * @return string
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,*.*";
274 $fp = null;
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);
284 if ($data) {
285 // with a PUT request the body must be written to a file stream
286 $fp = fopen('php://temp/maxmemory:256000', 'w');
287 if (! $fp) {
288 throw new Exception('Unable to write to php://temp for PUT request');
291 fwrite($fp, $data);
292 fseek($fp, 0);
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);
305 } else {
306 $full_url = $url . ($qs ? "?" . $qs : "");
307 $ch = curl_init($full_url);
310 curl_setopt($ch, CURLOPT_HTTPHEADER, array (
311 "Expect: "
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);
328 if ($fp) {
329 @fclose($fp); // if a PUT request had body data, close the file stream
332 if ($error != "") {
333 $tmp .= $error;
336 curl_close($ch);
338 return $tmp;
342 * Converts an array into a URL querystring
344 * @param
345 * array key/value pairs
346 * @return string
348 static function ArrayToQueryString($arr)
350 $qs = $arr;
352 if (is_array($arr)) {
353 // convert the data array into a url querystring
354 $qs = "";
355 $delim = "";
356 foreach (array_keys($arr) as $key) {
357 $qs .= $delim . $key . "=" . $arr [$key];
358 $delim = "&";
362 return $qs;