This article describes how to implement httpclient class in php. For more information, see
The code is as follows:
HttpClient: init ($ httpClient, $ args = null );
$ HttpClient-> get ($ url, $ data = null, $ cookie = null );
Var_dump ($ httpClient-> buffer );
The code is as follows:
Class httpClient {
Public $ buffer = null; // buffer gets the returned string
Public $ referer = null; // referer sets the HTTP_REFERER URL
Public $ response = null; // response header information of the response server
Public $ request = null; // The header information sent from the request 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 ['destgging']) {
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 ('destgging' => true, 'useragent' => 'msie 15.0 '));
$ HttpClient-> get ('http: // www.baidu.com ', 'Name = haowei ');
Echo $ httpClient-> request; // Obtain request header information
Echo $ httpClient-> response; // Obtain the response header information
Echo $ httpClient-> buffer; // Obtain webpage content
$ HttpClient-> get ('http: // www.bitsCN.com/ServiceLogin/', 'hash = '. $ time, 'uid = 1; users = admin ;')
Echo $ httpClient-> buffer;