PHP analog send POST request four, strengthen file_get_contents () Send POST request, filepostcontents_php Tutorial

Source: Internet
Author: User
Tags fread

PHP analog send POST request four, strengthen file_get_contents () Send POST request, filepostcontents


After using the cumbersome Fsockopen () method, we started looking for a simpler way to post requests in the PHP library, when we discovered that PHP's file functions also had the ability to interact with remote URLs.

The simplest is the fopen () and fread () functions.

$fp=fopen(' Http://localhost?query=query ', ' R '); $content=fread($fp, 1024x768); Echo $content; // Output HTML Document information fclose ($fp);

Then the file_get_contents () function:

$content=file_get_contents(' http://localhost?query=query '); Echo $content; // Output HTML Document information

However, we will find that in both ways we can only send information through get and read the Web page information, and these two ways are also faced with timeouts, unable to handle header information and other issues.

However, we look closely at the function prototypes of file_get_contents ():

String file_get_contents (String $filename [, bool $use _include_path [, Resource $context [, int $offset [, int $maxlen] ]]] )

We have found that it has other optional parameters, we can set the parameters, we send the page request, while the post of our data, the following to explain the meaning of the various parameters.

    • $filename: Needless to say, fill in the URL string we want to access.
    • $use _include_path: Whether to use the file before Include_path () set the path, if used, when the file address is not found, will automatically go to Include_path () set the path to find, the Web address we set to false.
    • $context: The environment context, the resource type, is set by the context returned by function stream_context_create (), and is the focus of our file_get_contents () function extension, and then again.
    • $offset: Read content relative to the beginning of the file offset bytes, we read the content of the Web page, to ensure the integrity of the HTML document, so can be set to 0 or not set, the default is 0.
    • $maxlen: As the name implies, is the maximum number of bytes to read the file, with offset we do not set, read the entire contents of the page.

The focus of sending the POST request via file_get_contents is on the $context parameter, and we set the context with the stream_context_create () function.

Stream_context_create () Creates a contextual item that can be used for a stream or for a file system. It is more useful for functions such as file_get_contents (), file_put_contents (), ReadFile () to use file name operations directly, without the handle of files. Stream_context_create () Adding headers is just a part of the function, you can also define proxies, timeouts, and so on.

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

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

As we can see, this function is used to get a contextual item for a resource type by passing in an array of settings.

$context stream_context_create (Array ( // $option parameter of the incoming array type Array (          set the array ' method ' = ' post ' with the HTTP request as the key  , // Set the request method to post ' Header '  = ' content-type:application/x-www-form-urlencoded ',// Set the post data format by setting the header file Http_build_query                        ($query _info), // Use the Http_build_query () method to combine the array data string ' timeout ' = 20    // Set the time-out for the request.   

Set the context, we submit the post data via the file_get_contents () function.

$results file_get_contents 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 (' method ' = ' POST ',' header ' = ' = ')                        Content-type:application/x-www-form-urlencoded ',        ' content ' = Html_build_query ($info),        ' Timeout ' = '))  ;   $result file_get_contents ($urlfalse$context);

If you feel that this article is helpful to you, you can look up or follow me, in addition, if there is any problem, you can leave a comment under the discussion, thank you.

http://www.bkjia.com/PHPjc/1067640.html www.bkjia.com true http://www.bkjia.com/PHPjc/1067640.html techarticle PHP analog send POST request four, strengthen file_get_contents () send the POST request, filepostcontents using the cumbersome Fsockopen () method, we began in the PHP function library to find a simpler side ...

  • 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.