PHP sends POST requests in simulation 4. enhances file_get_contents () to send POST requests. filepostcontents_PHP tutorial

Source: Internet
Author: User
PHP simulates POST request 4. enhances file_get_contents () to send POST request, filepostcontents. PHP simulates POST request 4. enhances file_get_contents () to send POST requests. after filepostcontents uses the bulky fsockopen () method, we started to look for a simpler method in the PHP function library to simulate POST request 4. enhanced file_get_contents () to send POST requests, filepostcontents

After using the bulky fsockopen () method, we began to look for a simpler method in the PHP function library for POST requests, we found that PHP file functions also have the ability to interact with remote URLs.

The simplest functions are fopen () and fread.

$ Fp = fopen ('http: // localhost? Query = query', 'r'); $ content = fread ($ fp, 1024); echo $ content; // output HTML document information fclose ($ fp );

Then there is the file_get_contents () function:

$ Content = file_get_contents ('http: // localhost? Query = query'); echo $ content; // output HTML document information

However, we will find that we can only send information and read the webpage information through the GET method through these two methods, and these two methods still face timeout, cannot handle header information and other issues.

However, let's take a closer look at the function prototype of file_get_contents:

string file_get_contents ( string $filename [, bool $use_include_path [, resource $context [, int $offset [, int $maxlen ]]]] )

We found that there are other optional parameters. we can set these parameters to POST our data while sending webpage requests. the meanings of these parameters are explained below.

  • $ Filename: Needless to say, just fill in the URL string we want to access.
  • $ Use_include_path: whether to use the path set by include_path () before the file. if this path is used, the path set by include_path () will be searched automatically when the file address cannot be found, set the webpage address to false.
  • $ Context: environment context and resource type, which are set by the context returned by the function stream_context_create (). This is also the focus of file_get_contents () function extension.
  • $ Offset: the offset byte of the read content relative to the start content of the file. to read the content of the webpage, you can set it to 0 or not. The default value is 0.
  • $ Maxlen: As the name implies, it is the maximum number of bytes to read the file. We do not set the offset to read all the content of the webpage.

The focus of the POST request sent through file_get_contents is on the $ context parameter. We use the stream_context_create () function to set the context.

The context options created by stream_context_create () can be used for stream or file system ). It is more useful for functions such as file_get_contents (), file_put_contents (), and readfile () that use file name operations without file handles. Stream_context_create () adds the header only as a part function, and can also define proxy and timeout.

Let's look at the prototype of the stream_context_create () function:

resource stream_context_create ([ array $options [, array $params ]] )

We can see that this function is used to obtain the context option of a resource type by passing in the set array.

$ Context = stream_context_create (array (// input the $ option parameter of the array type 'http' => array (// Set the array 'method' => 'Post with the http request as the key ', // Set the request method to POST 'head' => "Content-type: application/x-www-form-urlencoded ", // set the POST data format 'content' => http_build_query ($ query_info) by setting the header file, // use http_build_query () method: Combine the array into a data string 'timeout' => 20 // Set the request timeout time. )));

After setting the context, we use the file_get_contents () function to submit POST data.

$results = file_get_contents('http://localhost', false, $context); 

The following is a complete example of a POST request:

$info=['eat'=>'2kg','run'=>'10km'] ;$url='http://localhost';$context = stream_context_create(array(      'http' => array(          'method' => 'POST',          'header' => 'Content-type:application/x-www-form-urlencoded',          'content' => html_build_query($info),          'timeout' => 20      )  ));  $result = file_get_contents($url, false, $context);

If you think this article is helpful to you, please refer to me or follow me. if you have any questions, please leave a message below to discuss them. thank you.

Parse () sends a POST request. after filepostcontents uses the bulky fsockopen () method, we start to look for simpler methods in the PHP function library...

Related Article

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.