PHP function: stream_context_create () simulate POST/GET

Source: Internet
Author: User
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
)

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.