PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/ThinkPHP/Library/Vendor/HttpClient/HttpClient.php

https://gitlab.com/xuebutayan/yshop
PHP | 338 lines | 305 code | 1 blank | 32 comment | 49 complexity | 7f9160d945f10109bf67c8db1499677c MD5 | raw file
  1. <?php
  2. /* Version 0.9, 6th April 2003 - Simon Willison ( http://simon.incutio.com/ )
  3. Manual: http://scripts.incutio.com/httpclient/
  4. */
  5. namespace Vendor\HttpClient;
  6. class HttpClient {
  7. // Request vars
  8. var $host;
  9. var $port;
  10. var $path;
  11. var $method;
  12. var $postdata = '';
  13. var $cookies = array();
  14. var $referer;
  15. var $accept = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,image/jpeg,image/gif,*/*';
  16. var $accept_encoding = 'gzip';
  17. var $accept_language = 'en-us';
  18. var $user_agent = 'Incutio HttpClient v0.9';
  19. // Options
  20. var $timeout = 20;
  21. var $use_gzip = true;
  22. var $persist_cookies = true; // If true, received cookies are placed in the $this->cookies array ready for the next request
  23. // Note: This currently ignores the cookie path (and time) completely. Time is not important,
  24. // but path could possibly lead to security problems.
  25. var $persist_referers = true; // For each request, sends path of last request as referer
  26. var $debug = false;
  27. var $handle_redirects = true; // Auaomtically redirect if Location or URI header is found
  28. var $max_redirects = 5;
  29. var $headers_only = false; // If true, stops receiving once headers have been read.
  30. // Basic authorization variables
  31. var $username;
  32. var $password;
  33. // Response vars
  34. var $status;
  35. var $headers = array();
  36. var $content = '';
  37. var $errormsg;
  38. // Tracker variables
  39. var $redirect_count = 0;
  40. var $cookie_host = '';
  41. function HttpClient($host, $port=80) {
  42. $this->host = $host;
  43. $this->port = $port;
  44. }
  45. function get($path, $data = false) {
  46. $this->path = $path;
  47. $this->method = 'GET';
  48. if ($data) {
  49. $this->path .= '?'.$this->buildQueryString($data);
  50. }
  51. return $this->doRequest();
  52. }
  53. function post($path, $data) {
  54. $this->path = $path;
  55. $this->method = 'POST';
  56. $this->postdata = $this->buildQueryString($data);
  57. return $this->doRequest();
  58. }
  59. function buildQueryString($data) {
  60. $querystring = '';
  61. if (is_array($data)) {
  62. // Change data in to postable data
  63. foreach ($data as $key => $val) {
  64. if (is_array($val)) {
  65. foreach ($val as $val2) {
  66. $querystring .= urlencode($key).'='.urlencode($val2).'&';
  67. }
  68. } else {
  69. $querystring .= urlencode($key).'='.urlencode($val).'&';
  70. }
  71. }
  72. $querystring = substr($querystring, 0, -1); // Eliminate unnecessary &
  73. } else {
  74. $querystring = $data;
  75. }
  76. return $querystring;
  77. }
  78. function doRequest() {
  79. // Performs the actual HTTP request, returning true or false depending on outcome
  80. if (!$fp = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout)) {
  81. // Set error message
  82. switch($errno) {
  83. case -3:
  84. $this->errormsg = 'Socket creation failed (-3)';
  85. case -4:
  86. $this->errormsg = 'DNS lookup failure (-4)';
  87. case -5:
  88. $this->errormsg = 'Connection refused or timed out (-5)';
  89. default:
  90. $this->errormsg = 'Connection failed ('.$errno.')';
  91. $this->errormsg .= ' '.$errstr;
  92. $this->debug($this->errormsg);
  93. }
  94. return false;
  95. }
  96. socket_set_timeout($fp, $this->timeout);
  97. $request = $this->buildRequest();
  98. $this->debug('Request', $request);
  99. fwrite($fp, $request);
  100. // Reset all the variables that should not persist between requests
  101. $this->headers = array();
  102. $this->content = '';
  103. $this->errormsg = '';
  104. // Set a couple of flags
  105. $inHeaders = true;
  106. $atStart = true;
  107. // Now start reading back the response
  108. while (!feof($fp)) {
  109. $line = fgets($fp, 4096);
  110. if ($atStart) {
  111. // Deal with first line of returned data
  112. $atStart = false;
  113. if (!preg_match('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/', $line, $m)) {
  114. $this->errormsg = "Status code line invalid: ".htmlentities($line);
  115. $this->debug($this->errormsg);
  116. return false;
  117. }
  118. $http_version = $m[1]; // not used
  119. $this->status = $m[2];
  120. $status_string = $m[3]; // not used
  121. $this->debug(trim($line));
  122. continue;
  123. }
  124. if ($inHeaders) {
  125. if (trim($line) == '') {
  126. $inHeaders = false;
  127. $this->debug('Received Headers', $this->headers);
  128. if ($this->headers_only) {
  129. break; // Skip the rest of the input
  130. }
  131. continue;
  132. }
  133. if (!preg_match('/([^:]+):\\s*(.*)/', $line, $m)) {
  134. // Skip to the next header
  135. continue;
  136. }
  137. $key = strtolower(trim($m[1]));
  138. $val = trim($m[2]);
  139. // Deal with the possibility of multiple headers of same name
  140. if (isset($this->headers[$key])) {
  141. if (is_array($this->headers[$key])) {
  142. $this->headers[$key][] = $val;
  143. } else {
  144. $this->headers[$key] = array($this->headers[$key], $val);
  145. }
  146. } else {
  147. $this->headers[$key] = $val;
  148. }
  149. continue;
  150. }
  151. // We're not in the headers, so append the line to the contents
  152. $this->content .= $line;
  153. }
  154. fclose($fp);
  155. // If data is compressed, uncompress it
  156. if (isset($this->headers['content-encoding']) && $this->headers['content-encoding'] == 'gzip') {
  157. $this->debug('Content is gzip encoded, unzipping it');
  158. $this->content = substr($this->content, 10); // See http://www.php.net/manual/en/function.gzencode.php
  159. $this->content = gzinflate($this->content);
  160. }
  161. // If $persist_cookies, deal with any cookies
  162. if ($this->persist_cookies && isset($this->headers['set-cookie']) && $this->host == $this->cookie_host) {
  163. $cookies = $this->headers['set-cookie'];
  164. if (!is_array($cookies)) {
  165. $cookies = array($cookies);
  166. }
  167. foreach ($cookies as $cookie) {
  168. if (preg_match('/([^=]+)=([^;]+);/', $cookie, $m)) {
  169. $this->cookies[$m[1]] = $m[2];
  170. }
  171. }
  172. // Record domain of cookies for security reasons
  173. $this->cookie_host = $this->host;
  174. }
  175. // If $persist_referers, set the referer ready for the next request
  176. if ($this->persist_referers) {
  177. $this->debug('Persisting referer: '.$this->getRequestURL());
  178. $this->referer = $this->getRequestURL();
  179. }
  180. // Finally, if handle_redirects and a redirect is sent, do that
  181. if ($this->handle_redirects) {
  182. if (++$this->redirect_count >= $this->max_redirects) {
  183. $this->errormsg = 'Number of redirects exceeded maximum ('.$this->max_redirects.')';
  184. $this->debug($this->errormsg);
  185. $this->redirect_count = 0;
  186. return false;
  187. }
  188. $location = isset($this->headers['location']) ? $this->headers['location'] : '';
  189. $uri = isset($this->headers['uri']) ? $this->headers['uri'] : '';
  190. if ($location || $uri) {
  191. $url = parse_url($location.$uri);
  192. // This will FAIL if redirect is to a different site
  193. return $this->get($url['path']);
  194. }
  195. }
  196. return true;
  197. }
  198. function buildRequest() {
  199. $headers = array();
  200. $headers[] = "{$this->method} {$this->path} HTTP/1.0"; // Using 1.1 leads to all manner of problems, such as "chunked" encoding
  201. $headers[] = "Host: {$this->host}";
  202. $headers[] = "User-Agent: {$this->user_agent}";
  203. $headers[] = "Accept: {$this->accept}";
  204. if ($this->use_gzip) {
  205. $headers[] = "Accept-encoding: {$this->accept_encoding}";
  206. }
  207. $headers[] = "Accept-language: {$this->accept_language}";
  208. if ($this->referer) {
  209. $headers[] = "Referer: {$this->referer}";
  210. }
  211. // Cookies
  212. if ($this->cookies) {
  213. $cookie = 'Cookie: ';
  214. foreach ($this->cookies as $key => $value) {
  215. $cookie .= "$key=$value; ";
  216. }
  217. $headers[] = $cookie;
  218. }
  219. // Basic authentication
  220. if ($this->username && $this->password) {
  221. $headers[] = 'Authorization: BASIC '.base64_encode($this->username.':'.$this->password);
  222. }
  223. // If this is a POST, set the content type and length
  224. if ($this->postdata) {
  225. $headers[] = 'Content-Type: application/x-www-form-urlencoded';
  226. $headers[] = 'Content-Length: '.strlen($this->postdata);
  227. }
  228. $request = implode("\r\n", $headers)."\r\n\r\n".$this->postdata;
  229. return $request;
  230. }
  231. function getStatus() {
  232. return $this->status;
  233. }
  234. function getContent() {
  235. return $this->content;
  236. }
  237. function getHeaders() {
  238. return $this->headers;
  239. }
  240. function getHeader($header) {
  241. $header = strtolower($header);
  242. if (isset($this->headers[$header])) {
  243. return $this->headers[$header];
  244. } else {
  245. return false;
  246. }
  247. }
  248. function getError() {
  249. return $this->errormsg;
  250. }
  251. function getCookies() {
  252. return $this->cookies;
  253. }
  254. function getRequestURL() {
  255. $url = 'http://'.$this->host;
  256. if ($this->port != 80) {
  257. $url .= ':'.$this->port;
  258. }
  259. $url .= $this->path;
  260. return $url;
  261. }
  262. // Setter methods
  263. function setUserAgent($string) {
  264. $this->user_agent = $string;
  265. }
  266. function setAuthorization($username, $password) {
  267. $this->username = $username;
  268. $this->password = $password;
  269. }
  270. function setCookies($array) {
  271. $this->cookies = $array;
  272. }
  273. // Option setting methods
  274. function useGzip($boolean) {
  275. $this->use_gzip = $boolean;
  276. }
  277. function setPersistCookies($boolean) {
  278. $this->persist_cookies = $boolean;
  279. }
  280. function setPersistReferers($boolean) {
  281. $this->persist_referers = $boolean;
  282. }
  283. function setHandleRedirects($boolean) {
  284. $this->handle_redirects = $boolean;
  285. }
  286. function setMaxRedirects($num) {
  287. $this->max_redirects = $num;
  288. }
  289. function setHeadersOnly($boolean) {
  290. $this->headers_only = $boolean;
  291. }
  292. function setDebug($boolean) {
  293. $this->debug = $boolean;
  294. }
  295. // "Quick" static methods
  296. function quickGet($url) {
  297. $bits = parse_url($url);
  298. $host = $bits['host'];
  299. $port = isset($bits['port']) ? $bits['port'] : 80;
  300. $path = isset($bits['path']) ? $bits['path'] : '/';
  301. if (isset($bits['query'])) {
  302. $path .= '?'.$bits['query'];
  303. }
  304. $client = new HttpClient($host, $port);
  305. if (!$client->get($path)) {
  306. return false;
  307. } else {
  308. return $client->getContent();
  309. }
  310. }
  311. function quickPost($url, $data) {
  312. $bits = parse_url($url);
  313. $host = $bits['host'];
  314. $port = isset($bits['port']) ? $bits['port'] : 80;
  315. $path = isset($bits['path']) ? $bits['path'] : '/';
  316. $client = new HttpClient($host, $port);
  317. if (!$client->post($path, $data)) {
  318. return false;
  319. } else {
  320. return $client->getContent();
  321. }
  322. }
  323. function debug($msg, $object = false) {
  324. if ($this->debug) {
  325. print '<div style="border: 1px solid red; padding: 0.5em; margin: 0.5em;"><strong>HttpClient Debug:</strong> '.$msg;
  326. if ($object) {
  327. ob_start();
  328. print_r($object);
  329. $content = htmlentities(ob_get_contents());
  330. ob_end_clean();
  331. print '<pre>'.$content.'</pre>';
  332. }
  333. print '</div>';
  334. }
  335. }
  336. }
  337. ?>