PHP implementation HttpClient Class example _php instance

Source: Internet
Author: User
Copy CodeThe code is as follows:
Httpclient::init ($httpClient, $args = null);
$httpClient->get ($url, $data = null, $cookie = null);
Var_dump ($httpClient->buffer);

Copy the Code code as follows:

Class HttpClient {

public $buffer = null; Buffer gets the returned string
public $referer = null; Referer Setting the Http_referer URL
public $response = null; Header information for the 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);
}

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);
Header (' Content-type:text/plain; Charset=utf-8 ');
}

}

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. "\ r \ n";
$header. = ' Connection:close '. "\ r \ n";
$header. = ' Accept: */* '. "\ r \ n";
$header. = ' user-agent: '. (Isset ($this->args[' useragent ")? $this->args[' useragent ']: $_server[' http_user_agent ']). "\ r \ n";
$header. = ' dnt:1 '. "\ r \ n";
if ($cookie) $header. = ' Cookie: '. $cookie. "\ r \ n";
if ($this->referer) $header. = ' Referer: '. $this->referer. "\ r \ n";

$options = Array ();
$options [' http '] [' method '] = ' GET ';
$options [' http '] [' header '] = $header;

$response = Get_headers ($url);
$this->request = $header;
$this->response = Implode ("\ r \ n", $response);
$context = Stream_context_create ($options);
return $this->buffer = file_get_contents ($url, False, $context);

}

Public Function post ($url, $data = null, $cookie = null) {

$parse = Parse_url ($url);
$host = $parse [' Host '];

$header = ' Host: '. $host. "\ r \ n";
$header. = ' Connection:close '. "\ r \ n";
$header. = ' Accept: */* '. "\ r \ n";
$header. = ' user-agent: '. (Isset ($this->args[' useragent ")? $this->args[' useragent ']: $_server[' http_user_agent ']). "\ r \ n";
$header. = ' content-type:application/x-www-form-urlencoded '. "\ r \ n";
$header. = ' dnt:1 '. "\ r \ n";
if ($cookie) $header. = ' Cookie: '. $cookie. "\ r \ n";
if ($this->referer) $header. = ' Referer: '. $this->referer. "\ r \ n";
if ($data) $header. = ' Content-length: '. Strlen ($data). "\ r \ n";

$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 ("\ r \ n", $response);
$context = Stream_context_create ($options);
return $this->buffer = file_get_contents ($url, False, $context);

}

}

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; Get header information for a response
Echo $httpClient->buffer; Get Web page Content

$httpClient->get (' http://www.php.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.