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. Share to everyone for your reference, as follows:
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 ' = ' 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 code for http://localhost/test2.php is:
$data = $_post;echo '';p rint_r ($data); Echo '
';
The result of the operation is:
Array ( [foo] = bar [Baz] = Boom [site] + localhost [name] = Nowa Magic)
Some key points to explain:
1. The above procedure uses the Http_build_query () function, if you need to know, you can see the previous article "PHP using Http_build_query () to construct a URL string method."
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 (' 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,
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://localhost ', false, $context), all remaining data at the//fopen output file pointer:// Fpassthru ($FP); Fclose () before use
More readers interested in PHP related content can view this site: "Summary of PHP operations and operator usage," PHP Network Programming Tips Summary, "PHP Basic Grammar Primer Tutorial", "PHP operation Office Document tips summary (including word,excel,access, ppt), "PHP Date and Time usage summary", "PHP primer for Object-oriented programming", "PHP String Usage Summary", "Getting Started with Php+mysql database operations" and "PHP Common Database Operations Skills Summary"
I hope this article is helpful to you in PHP programming.
Articles you may be interested in:
- Example of using the PHP stream_context_create () function
- PHP stream_context_create () function and usage analysis
- PHP breaks through Http_referer-based anti-theft chain measures (stream_context_create)
- The method of using PHP Fsockopen to simulate post/get transmitting data
- PHP Custom class Fsocket method for simulating a post or GET request
- Simulating HTTP post and get requests using PHP Socket programming
- Example code in PHP that simulates post or get-submitted data in HTTP with a socket
- PHP Analog post| Get Operation Implementation Code
- PHP Curl analog Get and post function code
http://www.bkjia.com/PHPjc/1117036.html www.bkjia.com true http://www.bkjia.com/PHPjc/1117036.html techarticle PHP uses stream_context_create () to simulate Post/get request method, Streamcontext This example describes PHP using Stream_context_create () simulation post/ The method of the GET request. Share for the big ...