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:
<? PHP
$ 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, the official address is: http://scripts.incutio.com/httpclient/index.php
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 ('/');