Php post simulation behavior code summary_php tutorial

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

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.