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 ' = ' www.nowamagic.net ', ' 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 (in s)); $url = " Http://www.nowamagic.net/test2.php "; $context = Stream_context_create ($options); $result = file_get_contents ($url, False, $context); Echo $result;
The code for http://www.nowamagic.net/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] = www.nowamagic.net [name] = Nowa Magic)
Some key points to explain:
1. The above procedure uses the Http_build_query () function, if you need to understand, you can see the PHP function completion: http_build_query () constructs a URL string.
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 (Array (' http ' = = = Array (' method ' = ' ' POST ', ' header ')
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://www.nowamagic.net ', false, $context);// fopen all remaining data at the output file pointer://fpassthru ($FP); Fclose () before use
Transferred from: http://www.nowamagic.net/librarys/veda/detail/2585
php function Stream_context_create () analog post/get