PHP implementation HttpClient Class example

Source: Internet
Author: User

  This article mainly introduces the PHP implementation HttpClient Class example, the need for friends can refer to the following

  Code as follows: Httpclient::init ($httpClient, $args = null); $httpClient->get ($url, $data = null, $cookie = null); Var_dump ($httpClient->buffer);       Code as follows: <?php   class HttpClient {   public $buffer = null;  //buffer gets the returned string  public $referer = null;  //referer set http_referer URL  public $response = null; Header information for Response server response  public $request = null;  //Request header information sent to the server  private $args = null;    public static function init (& $instanceof, $args = Array ()) {  Return $instanceof = new self ($args); &NBSP}    private function __construct ($args = Array ()) {    if (!is_array ($args)) $args = Array ();   $this->args = $args;   if (!empty ($this->args[' debugging ')) {   ob_end_clean ();    set_time_limit (0);   & Nbsp;header (' Content-type:text/plain; Charset=utf-8 ');  }   &NBSP}    public function get ($urL, $data = null, $cookie = null) {    $parse = Parse_url ($url);   $url. = Isset ($parse [' query '])? ' & '. $data: ($data? '?'. $data: ");   $host = $parse [' Host '];     $header  = ' Host: '. $host. "RN";   $header. = ' Connection:close '. "RN";   $header. = ' Accept: */* '. "RN";   $header. = ' user-agent: '. (Isset ($this->args[' useragent '])? $this->args[' useragent ']: $_server[' http_user_agent ']). "RN";   $header. = ' dnt:1 '. "RN";   if ($cookie) $header. = ' Cookie: '. $cookie. "RN";   if ($this->referer) $header. = ' Referer: '. $this->referer. "RN";     $options = array ();   $options [' http '] [' method '] = ' get ';   $options [' http '] [' header '] = $header;     $response = Get_headers ($url);   $this->request = $header;   $this->response = implode ("rn", $response);   $context = stream_context_create ($options);   return $this->buffer = file_get_contents ($urL, False, $context);   &NBSP}    public function post ($url, $data = null, $cookie = null) {    $parse = Parse_url ($u RL);   $host = $parse [' Host '];     $header  = ' Host: '. $host. "RN";   $header. = ' Connection:close '. "RN";   $header. = ' Accept: */* '. "RN";   $header. = ' user-agent: '. (Isset ($this->args[' useragent '])? $this->args[' useragent ']: $_server[' http_user_agent ']). "RN";   $header. = ' content-type:application/x-www-form-urlencoded '. "RN";   $header. = ' dnt:1 '. "RN";   if ($cookie) $header. = ' Cookie: '. $cookie. "RN";   if ($this->referer) $header. = ' Referer: '. $this->referer. "RN";   if ($data) $header. = ' Content-length: '. Strlen ($data). "RN";     $options = array ();   $options [' http '] [' method '] = ' POST ';   $options [' http '] [' header '] = $header;   if ($data) $options [' http '] [' content '] = $data;     $response = Get_headers ($url);   $tHis->request = $header;   $this->response = implode ("rn", $response);   $context = stream_context_create ($options);   return $this->buffer = file_get_contents ($url, False, $context);   &NBSP}  }   Httpclient::init ($httpClient, Array (' Debugging ' => true, ' useragent ' => ' msie ' 15.0 ') )); $httpClient->get (' http://www.baidu.com ', ' Name=haowei '); Echo $httpClient->request; Get request header information echo $httpClient->response; Gets the header information for the response echo $httpClient->buffer; Get Web content   $httpClient->get (' http://www.jb51.net/ServiceLogin/', ' hash= '. $time, ' uid=1;users=admin; ')   Echo $httpClient->buffer;

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.