PHP uses stream_context_create () to simulate the POST/GET request method, streamcontext
This example describes how PHP uses stream_context_create () to simulate POST/GET requests. We will share this with you for your reference. The details are as follows:
Sometimes, we need to simulate POST/GET requests on the server, that is, simulation in the PHP program. How can we do this? Or, in the PHP program, how can I POST/GET this array to another address? Of course, it is easy to use CURL. What should I do if the CURL library is not used? In fact, related functions have been implemented in PHP. This function is stream_context_create () to be discussed next ().
Show you the code directly. This is the best method:
$ 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 http: // localhost/test2.php code is:
$data = $_POST;echo '<pre>';print_r( $data );echo '</pre>';
The running result is:
Array( [foo] => bar [baz] => boom [site] => localhost [name] => nowa magic)
Some key points:
1. The above program uses the http_build_query () function. For more information, see the previous article "using http_build_query () to construct a URL string in PHP".
2. stream_context_create () is used to create the options for opening up and down files, such as using POST access, using proxy, and sending headers. Create a stream. Let's take another example:
$context = stream_context_create(array( '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. The context option 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 directly 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. This makes web access not weaker than curl.
4. stream_context_create (): Creates and returns a text data stream and applies various options for fopen (), file_get_contents () special procedures for timeout settings, proxy server, request method, and header information settings.
5. stream_context_create can solve the file_get_contents timeout problem by adding the timeout option:
$ Opts = array ('http' => array ('method' => "GET", 'timeout' => 60 ,)); // create a data stream context $ context = stream_context_create ($ opts); $ html = file_get_contents ('HTTP: // localhost', false, $ context ); // all the remaining data at the fopen output file pointer: // fpassthru ($ fp); // used before fclose ()