PHP uses Stream_context_create () to simulate Post/get request method and Example analysis

Source: Internet
Author: User
This article mainly introduces the method that PHP uses Stream_context_create () to simulate Post/get request, and analyzes the principle of stream_context_create simulation Post/get request in detail with the example form. The use of methods and related considerations, the need for friends can refer to the following

Sometimes, we need to simulate post/get on the server side, that is, in the PHP program to implement the simulation, how to do it? Or, in a PHP program, give you an array, how do you post/get this array to another address? Of course, it's easy to use curl, so what if you don't use the Curl library? In fact, there are already related functions implemented in PHP, this function is the next stream_context_create ().

Direct show you the code, this is the best way:


$data = Array (    ' foo ' = ' bar ',     ' baz ' = ' boom ',     ' site ' = ' localhost ',     ' name ' = ' Nowa Magic ') ); $data = Http_build_query ($data); $postdata = Http_build_query ($data); $options = Array ('    http ' = = Array (        ' method ' = ' + ' POST ',        ' Header ' = ' content-type:application/x-www-form-urlencoded ',        ' Content ' + $data        //' Timeout ' = 60 * 60//Timeout (unit: s))    , $url = "http://localhost/test2.php"; $context = Stream_context_create ($options); $result = File_ Get_contents ($url, False, $context); Echo $result;


The code for http://localhost/test2.php is:


$data = $_post;echo ' <pre> ';p rint_r ($data); Echo ' </pre> ';


The result of the operation is:


Array (  [foo] = bar  [Baz] = Boom  [site] + localhost  [name] = Nowa Magic)


Some key points to explain:

1. The above procedure uses the Http_build_query () function, if you need to know, you can see the previous article "PHP using Http_build_query () to construct a URL string method."

2. Stream_context_create () is used to create an open file of the upper and lower file options, such as post access, using the proxy, send headers and so on. is to create a stream, one more example:


$context = stream_context_create ('     http ' = = Array (' method ' = ' +         ' POST ',         ' header ' = sprintf ( "Authorization:basic%s\r\n", Base64_encode ($username. ': ' $password)).         " content-type:application/x-www-form-urlencoded\r\n ",         ' content ' = http_build_query (Array (' status ' = $ Message)),         ' timeout ' = 5, ')     ; $ret = file_get_contents (' Http://twitter.com/statuses/update.xml ', false, $context);


3. 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 the files. Stream_context_create added header header is only a part of the function, you can also define agents, timeouts and so on. This makes the ability to access the Web to be not weaker than curl.

4. Stream_context_create () Role: Create and return a text stream and apply various options that can be used for fopen (), file_get_contents () and other procedures such as the time-out settings, Proxy server, request method, header information set up a special process.

5. Stream_context_create can also resolve file_get_contents timeout processing by adding the timeout option:


$opts = Array ('  http ' =>array (  ' method ' = ' = ' GET ',  ' timeout ' =>60,));//Create a data flow context $context = STREAM_ Context_create ($opts); $html =file_get_contents (' http://localhost ', false, $context), all remaining data at the//fopen output file pointer:// Fpassthru ($FP); Fclose () before use

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.