Php implementation httpclient class example _ PHP

Source: Internet
Author: User
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;

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.