HttpRequest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. /** @package verysimple::HTTP */
  3. /**
  4. * HttpRequest is a utility method for makine HTTP requests
  5. *
  6. * @package verysimple::HTTP
  7. * @author VerySimple Inc.
  8. * @copyright 1997-2007 VerySimple, Inc.
  9. * @license http://www.gnu.org/licenses/lgpl.html LGPL
  10. * @version 2.0
  11. */
  12. class HttpRequest
  13. {
  14. static $METHOD_GET = "GET";
  15. static $METHOD_POST = "POST";
  16. static $METHOD_PUT = "PUT";
  17. static $METHOD_DELETE = "DELETE";
  18. static $USER_AGENT = "verysimple::HttpRequest";
  19. static $VERIFY_CERT = false;
  20. /**
  21. *
  22. * @param $method
  23. * @param $params
  24. */
  25. static function RestRequest($endpoint, $method, $params = Array())
  26. {
  27. $qs = HttpRequest::ArrayToQueryString($params);
  28. $ch = null;
  29. switch ($method)
  30. {
  31. case HttpRequest::$METHOD_GET:
  32. $ch = curl_init($endpoint . ($qs ? "?" . $qs : ""));
  33. break;
  34. case HttpRequest::$METHOD_POST:
  35. $ch = curl_init($endpoint);
  36. curl_setopt($ch, CURLOPT_POST, 1);
  37. curl_setopt($ch, CURLOPT_POSTFIELDS, $qs);
  38. break;
  39. case HttpRequest::$METHOD_PUT:
  40. $ch = curl_init($endpoint);
  41. // curl_setopt($ch, CURLOPT_PUT, 1); // <- this method requires CURLOPT_INFILE
  42. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  43. curl_setopt($ch, CURLOPT_POSTFIELDS, $qs);
  44. break;
  45. case HttpRequest::$METHOD_DELETE:
  46. $ch = curl_init($endpoint);
  47. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
  48. curl_setopt($ch, CURLOPT_POSTFIELDS, $qs);
  49. break;
  50. }
  51. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect: ") ); //Fixes the HTTP/1.1 417 Expectation Failed Bug
  52. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  53. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  54. curl_setopt($ch, CURLOPT_VERBOSE, 0); // <- ENABLE DEBUGGING
  55. curl_setopt($ch, CURLOPT_USERAGENT, HttpRequest::$USER_AGENT);
  56. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, HttpRequest::$VERIFY_CERT);
  57. curl_setopt($ch, CURLOPT_NOPROGRESS, 1);
  58. // make the request
  59. $response = curl_exec ($ch);
  60. // if error is not empty, then a network error occured
  61. $error = curl_error($ch);
  62. if ($error) {$response .= $error;}
  63. curl_close ($ch);
  64. return $response;
  65. }
  66. /**
  67. * Make an HTTP POST request using the best method available on the server
  68. *
  69. * @param string $url
  70. * @param array $data (array of field/value pairs)
  71. * @param bool true to require verification of SSL cert
  72. * @return string
  73. */
  74. static function Post($url, $data, $verify_cert = false, $timeout = 30)
  75. {
  76. if (function_exists("curl_init"))
  77. {
  78. return HttpRequest::CurlPost($url, $data, $verify_cert, $timeout);
  79. }
  80. else
  81. {
  82. return HttpRequest::FilePost($url, $data, $verify_cert, $timeout);
  83. }
  84. }
  85. /**
  86. * Make an HTTP GET request using the best method available on the server
  87. *
  88. * @param string $url
  89. * @param array $data (array of field/value pairs)
  90. * @param bool true to require verification of SSL cert
  91. * @return string
  92. */
  93. static function Get($url, $data = "", $verify_cert = false, $timeout = 30)
  94. {
  95. if (function_exists("curl_init"))
  96. {
  97. return HttpRequest::CurlPost($url, $data, $verify_cert, $timeout);
  98. }
  99. else
  100. {
  101. return HttpRequest::FilePost($url, $data, $verify_cert, $timeout);
  102. }
  103. }
  104. /**
  105. * Make an HTTP PUT reequest using the best method available on the server
  106. *
  107. * @param string $url
  108. * @param array $data (array of field/value pairs)
  109. * @param bool true to require verification of SSL cert
  110. * @param int timeout (in seconds)
  111. * @return string
  112. */
  113. static function Put($url, $data = "", $verify_cert = false, $timeout = 30)
  114. {
  115. if (function_exists("curl_init"))
  116. {
  117. return HttpRequest::CurlPut($url, $data, $verify_cert, $timeout);
  118. }
  119. else
  120. {
  121. throw new Exception('PUT request is not supported on systems without curl installed');
  122. }
  123. }
  124. /**
  125. * Make an HTTP GET request using file_get_contents
  126. *
  127. * @param string $url
  128. * @param array $data (array of field/value pairs)
  129. * @param bool true to require verification of SSL cert
  130. * @return string
  131. */
  132. static function FileGet($url, $data = "", $verify_cert = false, $timeout = 30)
  133. {
  134. $qs = HttpRequest::ArrayToQueryString($data);
  135. $full_url = $url . ($qs ? "?" . $qs : "");
  136. return file_get_contents( $full_url );
  137. }
  138. /**
  139. * Make an HTTP POST request using file_get_contents
  140. *
  141. * @param string $url
  142. * @param array $data (array of field/value pairs)
  143. * @param bool true to require verification of SSL cert
  144. * @return string
  145. */
  146. static function FilePost($url, $data = "", $verify_cert = false, $timeout = 30)
  147. {
  148. $qs = HttpRequest::ArrayToQueryString($data);
  149. $url = $url . ($qs ? "?" . $qs : "");
  150. $show_headers = false;
  151. $url = parse_url($url);
  152. if (!isset($url['port'])) {
  153. if ($url['scheme'] == 'http') { $url['port']=80; }
  154. elseif ($url['scheme'] == 'https') { $url['port']=443; }
  155. }
  156. $url['query']=isset($url['query'])?$url['query']:'';
  157. $url['protocol']=$url['scheme'].'://';
  158. $eol="\r\n";
  159. $headers = "POST ".$url['protocol'].$url['host'].$url['path']." HTTP/1.0".$eol.
  160. "Host: ".$url['host'].$eol.
  161. "Referer: ".$url['protocol'].$url['host'].$url['path'].$eol.
  162. "Content-Type: application/x-www-form-urlencoded".$eol.
  163. "Content-Length: ".strlen($url['query']).$eol.
  164. $eol.$url['query'];
  165. $fp = fsockopen($url['host'], $url['port'], $errno, $errstr, 30);
  166. if($fp)
  167. {
  168. fputs($fp, $headers);
  169. $result = '';
  170. while(!feof($fp)) { $result .= fgets($fp, 128); }
  171. fclose($fp);
  172. if (!$show_headers)
  173. {
  174. //removes headers
  175. $match = preg_split("/\r\n\r\n/s",$result,2);
  176. $result = $match[1];
  177. }
  178. return $result;
  179. }
  180. }
  181. /**
  182. * Make an HTTP GET request using CURL
  183. *
  184. * @param string $url
  185. * @param variant $data querystring or array of field/value pairs
  186. * @param bool true to require verification of SSL cert
  187. * @return string
  188. */
  189. static function CurlGet($url, $data = "", $verify_cert = false, $timeout = 30)
  190. {
  191. return HttpRequest::CurlRequest("GET",$url, $data, $verify_cert, $timeout);
  192. }
  193. /**
  194. * Make an HTTP POST request using CURL
  195. *
  196. * @param string $url
  197. * @param variant $data querystring or array of field/value pairs
  198. * @param bool true to require verification of SSL cert
  199. * @return string
  200. */
  201. static function CurlPost($url, $data, $verify_cert = false, $timeout = 30)
  202. {
  203. return HttpRequest::CurlRequest("POST",$url, $data, $verify_cert, $timeout);
  204. }
  205. /**
  206. * Make an HTTP PUT request using CURL
  207. *
  208. * @param string $url
  209. * @param variant $data querystring or array of field/value pairs
  210. * @param bool true to require verification of SSL cert
  211. * @return string
  212. */
  213. static function CurlPut($url, $data, $verify_cert = false, $timeout = 30)
  214. {
  215. return HttpRequest::CurlRequest("PUT",$url, $data, $verify_cert, $timeout);
  216. }
  217. /**
  218. * Make an HTTP request using CURL
  219. *
  220. * @param string "POST" or "GET"
  221. * @param string $url
  222. * @param variant $data querystring or array of field/value pairs
  223. * @param bool true to require verification of SSL cert
  224. * @return string
  225. */
  226. static function CurlRequest($method, $url, $data, $verify_cert = false, $timeout = 30)
  227. {
  228. // if the data provided is in array format, convert it to a querystring
  229. $qs = HttpRequest::ArrayToQueryString($data);
  230. $agent = "verysimple::HttpRequest";
  231. // $header[] = "Accept: text/vnd.wap.wml,*.*";
  232. $fp = null;
  233. if ($method == "POST")
  234. {
  235. $ch = curl_init($url);
  236. curl_setopt($ch, CURLOPT_POST, 1);
  237. curl_setopt($ch, CURLOPT_POSTFIELDS, $qs);
  238. }
  239. elseif ($method == 'PUT')
  240. {
  241. $ch = curl_init($url);
  242. curl_setopt($ch, CURLOPT_PUT, true);
  243. if ($data)
  244. {
  245. // with a PUT request the body must be written to a file stream
  246. $fp = fopen('php://temp/maxmemory:256000', 'w');
  247. if (!$fp) {
  248. throw new Exception('Unable to write to php://temp for PUT request');
  249. }
  250. fwrite($fp, $data);
  251. fseek($fp, 0);
  252. // if the PUT request contains JSON data then add the content type header
  253. if (json_encode($data)) curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json") );
  254. curl_setopt($ch, CURLOPT_INFILE, $fp);
  255. curl_setopt($ch, CURLOPT_INFILESIZE, strlen($data));
  256. curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
  257. }
  258. }
  259. else
  260. {
  261. $full_url = $url . ($qs ? "?" . $qs : "");
  262. $ch = curl_init($full_url);
  263. }
  264. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect: ") ); //Fixes the HTTP/1.1 417 Expectation Failed Bug
  265. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  266. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  267. curl_setopt($ch, CURLOPT_VERBOSE, 0); ########### debug
  268. curl_setopt($ch, CURLOPT_USERAGENT, $agent);
  269. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verify_cert);
  270. curl_setopt($ch, CURLOPT_NOPROGRESS, 1);
  271. // curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  272. // curl_setopt($ch, CURLOPT_COOKIEJAR, "curl_cookie");
  273. // curl_setopt($ch, CURLOPT_COOKIEFILE, "curl_cookie");
  274. curl_setopt($ch, CURLOPT_TIMEOUT,$timeout);
  275. $tmp = curl_exec ($ch);
  276. $error = curl_error($ch);
  277. if ($fp) @fclose($fp); // if a PUT request had body data, close the file stream
  278. if ($error != "") {$tmp .= $error;}
  279. curl_close ($ch);
  280. return $tmp;
  281. }
  282. /**
  283. * Converts an array into a URL querystring
  284. * @param array key/value pairs
  285. * @return string
  286. */
  287. static function ArrayToQueryString($arr)
  288. {
  289. $qs = $arr;
  290. if (is_array($arr))
  291. {
  292. // convert the data array into a url querystring
  293. $qs = "";
  294. $delim = "";
  295. foreach (array_keys($arr) as $key)
  296. {
  297. $qs .= $delim . $key ."=" . $arr[$key];
  298. $delim = "&";
  299. }
  300. }
  301. return $qs;
  302. }
  303. }
  304. ?>