GET behavior is relatively simple, and POST is more complicated. There are two ways to choose from: first, handwriting code. Second: The first method to use the HttpClientphp class library: PHP code? PHP $ flag = 0; // the data to post $ argv = array ('var1' = 'abc', 'var2' = ''); // Construct the string to post
GET behavior is relatively simple, and POST is more complicated. There are two ways to choose from: first, handwriting code. Second: use the HttpClient php class library
Method 1:
PHP code
<? 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.
PHP code
$ PageContents = HttpClient: quickPost ('http: // example.com/someForm', array (
'Name' => 'some name ',
'Email '=> 'email @ example.com'
));
Use the httpclient class library, you can go to the official download of the latest class library, the official address: http://scripts.incutio.com/httpclient/index.php
Additional php httpclient usage
Static method to obtain the webpage:
PHP code
$ PageContents = HttpClient: quickGet ('http: // example.com /');
Get method
PHP code
$ Client = new HttpClient ('example. com ');
If (! $ Client-> get ('/')){
Die ('An error occurred: '. $ client-> getError ());
}
$ PageContents = $ client-> getContent ();
Get the Get method with debugging
PHP code
$ 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
PHP code
$ 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
PHP code
$ 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
PHP code
$ 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
PHP code
$ 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
PHP code
$ Client = new HttpClient ('example. com ');
$ Client-> setAuthorization ('username', 'password ');
If (! $ Client-> get ('/')){
Die ('An error occurred: '. $ client-> getError ());
}
$ PageContents = $ client-> getContent ();
Output header information
PHP code
$ 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.
PHP code
$ Client = new HttpClient ('www .amazon.com ');
$ Client-> setDebug (true );
$ Client-> setMaxRedirects (3 );
$ Client-> get ('/');