PHP's HttpClient class

Source: Internet
Author: User
  1. Class HttpClient {
  2. public $buffer = null; Buffer gets the returned string
  3. public $referer = null; Referer Setting the Http_referer URL
  4. public $response = null; Header information for the response server response
  5. public $request = null; Request header information sent to the server
  6. Private $args = null;
  7. public static function init (& $instanceof, $args = Array ()) {
  8. return $instanceof = new self ($args);
  9. }
  10. Private Function __construct ($args = Array ()) {
  11. if (!is_array ($args)) $args = Array ();
  12. $this->args = $args;
  13. if (!empty ($this->args[' debugging ')) {
  14. Ob_end_clean ();
  15. Set_time_limit (0);
  16. Header (' Content-type:text/plain; Charset=utf-8 ');
  17. }
  18. }
  19. Public function Get ($url, $data = null, $cookie = null) {
  20. $parse = Parse_url ($url);
  21. $url. = Isset ($parse [' query '])? ' & '. $data: ($data? '?'. $data: ");
  22. $host = $parse [' Host '];
  23. $header = ' Host: '. $host. "\\r\\n";
  24. $header. = ' Connection:close '. "\\r\\n";
  25. $header. = ' Accept: */* '. "\\r\\n";
  26. $header. = ' user-agent: '. (Isset ($this->args[' useragent ")? $this->args[' useragent ']: $_server[' http_user_agent ']). "\\r\\n";
  27. $header. = ' dnt:1 '. "\\r\\n";
  28. if ($cookie) $header. = ' Cookie: '. $cookie. "\\r\\n";
  29. if ($this->referer) $header. = ' Referer: '. $this->referer. "\\r\\n";
  30. $options = Array ();
  31. $options [' http '] [' method '] = ' GET ';
  32. $options [' http '] [' header '] = $header;
  33. $response = Get_headers ($url);
  34. $this->request = $header;
  35. $this->response = Implode ("\\r\\n", $response);
  36. $context = Stream_context_create ($options);
  37. return $this->buffer = file_get_contents ($url, False, $context);
  38. }
  39. Public Function post ($url, $data = null, $cookie = null) {
  40. $parse = Parse_url ($url);
  41. $host = $parse [' Host '];
  42. $header = ' Host: '. $host. "\\r\\n";
  43. $header. = ' Connection:close '. "\\r\\n";
  44. $header. = ' Accept: */* '. "\\r\\n";
  45. $header. = ' user-agent: '. (Isset ($this->args[' useragent ")? $this->args[' useragent ']: $_server[' http_user_agent ']). "\\r\\n";
  46. $header. = ' content-type:application/x-www-form-urlencoded '. "\\r\\n";
  47. $header. = ' dnt:1 '. "\\r\\n";
  48. if ($cookie) $header. = ' Cookie: '. $cookie. "\\r\\n";
  49. if ($this->referer) $header. = ' Referer: '. $this->referer. "\\r\\n";
  50. if ($data) $header. = ' Content-length: '. Strlen ($data). "\\r\\n";
  51. $options = Array ();
  52. $options [' http '] [' method '] = ' POST ';
  53. $options [' http '] [' header '] = $header;
  54. if ($data) $options [' http '] [' content '] = $data;
  55. $response = Get_headers ($url);
  56. $this->request = $header;
  57. $this->response = Implode ("\\r\\n", $response);
  58. $context = Stream_context_create ($options);
  59. return $this->buffer = file_get_contents ($url, False, $context);
  60. }
  61. }
  62. Httpclient::init ($httpClient, Array (' debugging ' = = True, ' useragent ' = ' MSIE 15.0 '));
  63. $httpClient->get (' http://www.baidu.com ', ' Name=haowei ');
  64. Echo $httpClient->request; Get Request header information
  65. Echo $httpClient->response; Get header information for a response
  66. Echo $httpClient->buffer; Get Web page Content
  67. $httpClient->get (' http://accounts.haowei.me/ServiceLogin/', ' hash= '. $time, ' uid=1;users=admin; ')
  68. Echo $httpClient->buffer;
Copy Code
PHP, HttpClient
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.