PHP function: stream_context_create () simulates POSTGET. sometimes, we need to simulate POST/GET requests on the server side, that is, simulate requests 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.
'Bar', 'Baz' => 'boom ', 'site' => 'Www .example.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 (unit: s ))); $ url = "http: // 127.0.0.1/test2.php"; $ context = stream_context_create ($ options); $ result = File_get_contents ($ url, false, $ context); echo $ result;?>
The code for test2.php is:
';print_r( $data );echo '
';?>
The running result is:
Array
(
[Foo] => bar
[Baz] => boom
[Site] => www.example.net
[Name] => nowa magic
)