: This article mainly introduces the PHP practical function: stream_context_create simulate POSTGET. if you are interested in the PHP Tutorial, refer to it.
PHP practical function Manual: stream_context_create () simulate POST/GET
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' => 'Www .lai18.com ', 'name' => 'lai18'); $ 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://www.lai18.net/"; $ context = stream_context_create ($ options); $ result = file_get_contents ($ url, false, $ context); echo $ result;
The http://www.lai18.com code is:
$ Data = $ _ POST; echo'
';print_r( $data );echo '
';
The running result is:
Array ([foo] => bar [baz] => boom [site] => www.lai18.com [name] => lai18)
Some important points (reference: http://www.lai18.com/content/425476.html ):
1. the above program uses the http_build_query () function. For more information, see stream. It can also be used in a 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: // www.lai18.com ', false, $ context ); // all the remaining data at the fopen output file pointer: // fpassthru ($ fp); // used before fclose ()
Additional reading
The topic list of this article is as follows:
Complete the 1PHP function: get_magic_quotes_gpc ()
2PHP function completion: error_reporting ()
Complete the 3PHP function: array_splice ()
Complete the 4PHP function: isset ()
5PHP function completion: array_multisort ()
6 PHP function completion: urlencode ()
7 PHP function completion: preg_match ()
Complete the 8PHP function: header ()
9PHP function completion: getenv ()
10PHP function completion: list ()
Complete the 11PHP function: mysql_num_rows ()
12PHP function completion: explode ()
Complete the 13PHP function: mysql_fetch_array ()
Complete the 14PHP function: number_format ()
After completing the 15PHP function: mysql_query ()
16PHP function completion: empty ()
Complete the 17PHP function: call_user_func ()
Complete the 18PHP function: parse_url ()
19PHP function completion: import_request_variables ()
Complete the 20PHP function: ImageCopyResamples ()
21PHP function completion: remove the HTML tag strip_tags ()
22PHP function completion: output array structure and content var_dump ()
Complete the 23PHP function: var_export ()
Complete the 24PHP function: Determine whether the variable is a number is_numeric ()
25PHP function completion: http_build_query () construct the URL string
26PHP practical function Manual: stream_context_create () simulate POST/GET
Complete the 27PHP function: shuffle () is used to obtain several random elements in the array.
Completion of the 28PHP function: session_id ()
29PHP function completion: nl2br () and nl2p () functions
30PHP function completion: session_name ()
How to implement the ignore_user_abort function for task scheduling in 31php
How to use the error_log () function in 32PHP
33PHP practical function-array_change_key_case () converts the array key to uppercase and lowercase.
34PHP utility function-function_exists () check whether the function has been defined
35PHP practical function-array_intersect () calculates the intersection of two or more arrays
36PHP practical function-array_diff () calculates the difference set between arrays
37PHP practical function-compact () converts known variables into arrays
38PHP practical function-extract () converts the array to the corresponding key-value pair variable
39PHP utility-array_count_values (): counts the number of times an element appears in an array.
40PHP practical function-getopt () get options from the command line parameter list
41PHP practical functions String function classification
42 use the filter function provided by PHP for data verification
The above describes the PHP practical function: stream_context_create simulate POST/GET, including some content, hope to be helpful to friends who are interested in PHP tutorials.