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;