HttpClientphp class library-make a little progress every day-blog channel-PHP source code

Source: Internet
Author: User
Python basic data structure: list, tuples and dictionaries 1. list a set of ordered projects. The variable data type [add, delete, modify, and query] list is a data set surrounded by square brackets "[]". different members are separated by commas. The list can contain any data type or another list. you can access the members of the list by serial number.

HttpClient php class library


GET behavior is relatively simple, and POST is more complicated. Two methods are provided here:

1. handwritten code
Second: use the HttpClient php class library

Method 1: The code is as follows:
$ Flag = 0;
// Data to be post
$ Argv = array (
'Var1' => 'ABC ',
'Var2' => '');
// Construct the string to post
Foreach ($ argv as $ key => $ value ){
If ($ flag! = 0 ){
$ Params. = "&";
$ Flag = 1;
}
$ Params. = $ key. "="; $ params. = urlencode ($ value );
$ Flag = 1;
}
$ Length = strlen ($ params );
// Create a socket connection
$ Fp = fsockopen ("127.0.0.1", 80, $ errno, $ errstr, 10) or exit ($ errstr. "--->". $ errno );
// Construct the post request header
$ Header = "POST/mobile/try. php HTTP/1.1 ";
$ Header. = "Host: 127.0.0.1 ";
$ Header. = "Referer:/mobile/sendpost. php ";
$ Header. = "Content-Type: application/x-www-form-urlencoded ";
$ Header. = "Content-Length:". $ length ."";
$ Header. = "Connection: Close ";
// Add the post string
$ Header. = $ params ."";
// Send post data
Fputs ($ fp, $ header );
$ Inheader = 1;
While (! Feof ($ fp )){
$ Line = fgets ($ fp, 1024); // only the returned data on the page is displayed when the request packet header is removed.
If ($ inheader & ($ line = "\ n" | $ line = "")){
$ Inheader = 0;
}
If ($ inheader = 0 ){
Echo $ line;
}
}
Fclose ($ fp );
?>

The second method is to use the httpclient class. the code is as follows:
$ PageContents = HttpClient: quickPost ('http: // example.com/someForm', array (
'Name' => 'some name ',
'Email '=> 'email @ example.com'
));

Use the library attachment, you can also go to the official download the latest class library, official address: https://www.php1.cn/
Additional php httpclient usage
Static method for obtaining web pages
$ PageContents = HttpClient: quickGet ('http: // example.com /');

Get method
$ Client = new HttpClient ('example. com ');
If (! $ Client-> get ('/')){
Die ('An error occurred: '. $ client-> getError ());
}
$ PageContents = $ client-> getContent ();

Get the Get method with debugging
$ Client = new HttpClient ('example. com ');
$ Client-> setDebug (true );
If (! $ Client-> get ('/')){
Die ('An error occurred: '. $ client-> getError ());
}
$ PageContents = $ client-> getContent ();

Get method with automatic steering
$ Client = new HttpClient ('www .amazon.com ');
$ Client-> setDebug (true );
If (! $ Client-> get ('/')){
Die ('An error occurred: '. $ client-> getError ());
}
$ PageContents = $ client-> getContent ();

Check whether the page exists
$ Client = new HttpClient ('example. com ');
$ Client-> setDebug (true );
If (! $ Client-> get ('/thispagedoesnotexist ')){
Die ('An error occurred: '. $ client-> getError ());
}
If ($ client-> getStatus () = '000000 '){
Echo 'page does not exist! ';
}
$ PageContents = $ client-> getContent ();

Counterfeit client
$ Client = new HttpClient ('example. com ');
$ Client-> setDebug (true );
$ Client-> setUserAgent ('mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: 1.3a) Gecko/8080 ');
If (! $ Client-> get ('/')){
Die ('An error occurred: '. $ client-> getError ());
}
$ PageContents = $ client-> getContent ();

Log on to verify and request a webpage
$ Client = new HttpClient ('example. com ');
$ Client-> post ('/login. php', array (
'Username' => 'Simon ',
'Password' => 'ducks'
));
If (! $ Client-> get ('/private. php ')){
Die ('An error occurred: '. $ client-> getError ());
}
$ PageContents = $ client-> getContent ();

HTTP authorization
$ Client = new HttpClient ('example. com ');
$ Client-> setAuthorization ('username', 'password ');
If (! $ Client-> get ('/')){
Die ('An error occurred: '. $ client-> getError ());
}
$ PageContents = $ client-> getContent ();

Output header information
$ Client = new HttpClient ('example. com ');
If (! $ Client-> get ('/')){
Die ('An error occurred: '. $ client-> getError ());
}
Print_r ($ client-> getHeaders ());

Sets the maximum number of domain redirection times.
$ Client = new HttpClient ('www .amazon.com ');
$ Client-> setDebug (true );
$ Client-> setMaxRedirects (3 );
$ Client-> get ('/');

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.