Httpclient.class.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. class Httpclient
  3. {
  4. // Request vars
  5. var $host;
  6. var $port;
  7. var $path;
  8. var $method;
  9. var $postdata = '';
  10. var $cookies = array();
  11. var $referer;
  12. var $accept = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,image/jpeg,image/gif,*/*';
  13. var $accept_encoding = 'gzip';
  14. var $accept_language = 'en-us';
  15. var $user_agent = 'Incutio HttpClient v0.9';
  16. var $timeout = 20;
  17. var $use_gzip = true;
  18. var $persist_cookies = true;
  19. var $persist_referers = true;
  20. var $debug = false;
  21. var $handle_redirects = true;
  22. var $max_redirects = 5;
  23. var $headers_only = false;
  24. var $username;
  25. var $password;
  26. var $status;
  27. var $headers = array();
  28. var $content = '';
  29. var $errormsg;
  30. var $redirect_count = 0;
  31. var $cookie_host = '';
  32. function __construct($host, $port=80) {
  33. $this->host = $host;
  34. $this->port = $port;
  35. }
  36. function get($path, $data = false) {
  37. $this->path = $path;
  38. $this->method = 'GET';
  39. if ($data) {
  40. $this->path .= '?'.$this->buildQueryString($data);
  41. }
  42. return $this->doRequest();
  43. }
  44. function post($path, $data) {
  45. $this->path = $path;
  46. $this->method = 'POST';
  47. $this->postdata = $this->buildQueryString($data);
  48. return $this->doRequest();
  49. }
  50. function buildQueryString($data) {
  51. $querystring = '';
  52. if (is_array($data)) {
  53. foreach ($data as $key => $val) {
  54. if (is_array($val)) {
  55. foreach ($val as $val2) {
  56. $querystring .= urlencode($key).'='.urlencode($val2).'&';
  57. }
  58. } else {
  59. $querystring .= urlencode($key).'='.urlencode($val).'&';
  60. }
  61. }
  62. $querystring = substr($querystring, 0, -1); // Eliminate unnecessary &
  63. } else {
  64. $querystring = $data;
  65. }
  66. return $querystring;
  67. }
  68. function doRequest() {
  69. if (!$fp = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout)) {
  70. switch($errno) {
  71. case -3:
  72. $this->errormsg = 'Socket creation failed (-3)';
  73. case -4:
  74. $this->errormsg = 'DNS lookup failure (-4)';
  75. case -5:
  76. $this->errormsg = 'Connection refused or timed out (-5)';
  77. default:
  78. $this->errormsg = 'Connection failed ('.$errno.')';
  79. $this->errormsg .= ' '.$errstr;
  80. $this->debug($this->errormsg);
  81. }
  82. return false;
  83. }
  84. socket_set_timeout($fp, $this->timeout);
  85. $request = $this->buildRequest();
  86. $this->debug('Request', $request);
  87. fwrite($fp, $request);
  88. $this->headers = array();
  89. $this->content = '';
  90. $this->errormsg = '';
  91. $inHeaders = true;
  92. $atStart = true;
  93. while (!feof($fp)) {
  94. $line = fgets($fp, 4096);
  95. if ($atStart) {
  96. $atStart = false;
  97. if (!preg_match('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/', $line, $m)) {
  98. $this->errormsg = "Status code line invalid: ".htmlentities($line);
  99. $this->debug($this->errormsg);
  100. return false;
  101. }
  102. $http_version = $m[1];
  103. $this->status = $m[2];
  104. $status_string = $m[3];
  105. $this->debug(trim($line));
  106. continue;
  107. }
  108. if ($inHeaders) {
  109. if (trim($line) == '') {
  110. $inHeaders = false;
  111. $this->debug('Received Headers', $this->headers);
  112. if ($this->headers_only) {
  113. break;
  114. }
  115. continue;
  116. }
  117. if (!preg_match('/([^:]+):\\s*(.*)/', $line, $m)) {
  118. continue;
  119. }
  120. $key = strtolower(trim($m[1]));
  121. $val = trim($m[2]);
  122. if (isset($this->headers[$key])) {
  123. if (is_array($this->headers[$key])) {
  124. $this->headers[$key][] = $val;
  125. } else {
  126. $this->headers[$key] = array($this->headers[$key], $val);
  127. }
  128. } else {
  129. $this->headers[$key] = $val;
  130. }
  131. continue;
  132. }
  133. $this->content .= $line;
  134. }
  135. fclose($fp);
  136. if (isset($this->headers['content-encoding']) && $this->headers['content-encoding'] == 'gzip') {
  137. $this->debug('Content is gzip encoded, unzipping it');
  138. $this->content = substr($this->content, 10);
  139. $this->content = gzinflate($this->content);
  140. }
  141. if ($this->persist_cookies && isset($this->headers['set-cookie']) && $this->host == $this->cookie_host) {
  142. $cookies = $this->headers['set-cookie'];
  143. if (!is_array($cookies)) {
  144. $cookies = array($cookies);
  145. }
  146. foreach ($cookies as $cookie) {
  147. if (preg_match('/([^=]+)=([^;]+);/', $cookie, $m)) {
  148. $this->cookies[$m[1]] = $m[2];
  149. }
  150. }
  151. $this->cookie_host = $this->host;
  152. }
  153. if ($this->persist_referers) {
  154. $this->debug('Persisting referer: '.$this->getRequestURL());
  155. $this->referer = $this->getRequestURL();
  156. }
  157. if ($this->handle_redirects) {
  158. if (++$this->redirect_count >= $this->max_redirects) {
  159. $this->errormsg = 'Number of redirects exceeded maximum ('.$this->max_redirects.')';
  160. $this->debug($this->errormsg);
  161. $this->redirect_count = 0;
  162. return false;
  163. }
  164. $location = isset($this->headers['location']) ? $this->headers['location'] : '';
  165. $uri = isset($this->headers['uri']) ? $this->headers['uri'] : '';
  166. if ($location || $uri) {
  167. $url = parse_url($location.$uri);
  168. return $this->get($url['path']);
  169. }
  170. }
  171. return true;
  172. }
  173. function buildRequest() {
  174. $headers = array();
  175. $headers[] = "{$this->method} {$this->path} HTTP/1.0";
  176. $headers[] = "Host: {$this->host}";
  177. $headers[] = "User-Agent: {$this->user_agent}";
  178. $headers[] = "Accept: {$this->accept}";
  179. if ($this->use_gzip) {
  180. $headers[] = "Accept-encoding: {$this->accept_encoding}";
  181. }
  182. $headers[] = "Accept-language: {$this->accept_language}";
  183. if ($this->referer) {
  184. $headers[] = "Referer: {$this->referer}";
  185. }
  186. if ($this->cookies) {
  187. $cookie = 'Cookie: ';
  188. foreach ($this->cookies as $key => $value) {
  189. $cookie .= "$key=$value; ";
  190. }
  191. $headers[] = $cookie;
  192. }
  193. if ($this->username && $this->password) {
  194. $headers[] = 'Authorization: BASIC '.base64_encode($this->username.':'.$this->password);
  195. }
  196. if ($this->postdata) {
  197. $headers[] = 'Content-Type: application/x-www-form-urlencoded';
  198. $headers[] = 'Content-Length: '.strlen($this->postdata);
  199. }
  200. $request = implode("\r\n", $headers)."\r\n\r\n".$this->postdata;
  201. return $request;
  202. }
  203. function getStatus() {
  204. return $this->status;
  205. }
  206. function getContent() {
  207. return $this->content;
  208. }
  209. function getHeaders() {
  210. return $this->headers;
  211. }
  212. function getHeader($header) {
  213. $header = strtolower($header);
  214. if (isset($this->headers[$header])) {
  215. return $this->headers[$header];
  216. } else {
  217. return false;
  218. }
  219. }
  220. function getError() {
  221. return $this->errormsg;
  222. }
  223. function getCookies() {
  224. return $this->cookies;
  225. }
  226. function getRequestURL() {
  227. $url = 'http://'.$this->host;
  228. if ($this->port != 80) {
  229. $url .= ':'.$this->port;
  230. }
  231. $url .= $this->path;
  232. return $url;
  233. }
  234. function setUserAgent($string) {
  235. $this->user_agent = $string;
  236. }
  237. function setAuthorization($username, $password) {
  238. $this->username = $username;
  239. $this->password = $password;
  240. }
  241. function setCookies($array) {
  242. $this->cookies = $array;
  243. }
  244. function useGzip($boolean) {
  245. $this->use_gzip = $boolean;
  246. }
  247. function setPersistCookies($boolean) {
  248. $this->persist_cookies = $boolean;
  249. }
  250. function setPersistReferers($boolean) {
  251. $this->persist_referers = $boolean;
  252. }
  253. function setHandleRedirects($boolean) {
  254. $this->handle_redirects = $boolean;
  255. }
  256. function setMaxRedirects($num) {
  257. $this->max_redirects = $num;
  258. }
  259. function setHeadersOnly($boolean) {
  260. $this->headers_only = $boolean;
  261. }
  262. function setDebug($boolean) {
  263. $this->debug = $boolean;
  264. }
  265. function quickGet($url) {
  266. $bits = parse_url($url);
  267. $host = $bits['host'];
  268. $port = isset($bits['port']) ? $bits['port'] : 80;
  269. $path = isset($bits['path']) ? $bits['path'] : '/';
  270. if (isset($bits['query'])) {
  271. $path .= '?'.$bits['query'];
  272. }
  273. $client = new HttpClient($host, $port);
  274. if (!$client->get($path)) {
  275. return false;
  276. } else {
  277. return $client->getContent();
  278. }
  279. }
  280. function quickPost($url, $data) {
  281. $bits = parse_url($url);
  282. $host = $bits['host'];
  283. $port = isset($bits['port']) ? $bits['port'] : 80;
  284. $path = isset($bits['path']) ? $bits['path'] : '/';
  285. $client = new HttpClient($host, $port);
  286. if (!$client->post($path, $data)) {
  287. return false;
  288. } else {
  289. return $client->getContent();
  290. }
  291. }
  292. function debug($msg, $object = false) {
  293. if ($this->debug) {
  294. print '<div style="border: 1px solid red; padding: 0.5em; margin: 0.5em;"><strong>HttpClient Debug:</strong> '.$msg;
  295. if ($object) {
  296. ob_start();
  297. print_r($object);
  298. $content = htmlentities(ob_get_contents());
  299. ob_end_clean();
  300. print '<pre>'.$content.'</pre>';
  301. }
  302. print '</div>';
  303. }
  304. }
  305. }
  306. ?>