Php post simulation code summary. 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 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 => how are you );
// 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 = "" | $ line = "")){
$ Inheader = 0;
}
If ($ inheader = 0 ){
Echo $ line;
}
}
Fclose ($ fp );
?>
The second method is to use the httpclient class.
PHP code
$ PageContents = HttpClient: quick post (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 () = 404 ){
Echo Page does not exist !;
}
$ PageContents = $ client-> getContent ();
Counterfeit client
PHP code
$ Client = new HttpClient (exa
Bytes. There are two ways to choose from: first, handwriting code. Second: The first method to use the HttpClient php class library: PHP code? PHP...