PHP function Completion: stream_context_create () Analog post/get
Creating a PHP stream
On January 08, 2011 That day has been written 9,408 times to read the Thank you reference or the original server has spent a total of 94.075 MS 3 database queries, and strive to provide you with this page.Try reading mode? I'd like to hear your suggestions.
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:
04 |
‘site‘ => ‘www.nowamagic.net‘ , |
05 |
‘name‘ => ‘nowa magic‘ ); |
07 |
$data = http_build_query( $data ); |
09 |
//$postdata = http_build_query($data); |
13 |
‘header‘ => ‘Content-type:application/x-www-form-urlencoded‘ , |
15 |
//‘timeout‘ => 60 * 60 // 超时时间(单位:s) |
19 |
$url = "http://www.nowamagic.net/test2.php" ; |
20 |
$context = stream_context_create( $options ); |
21 |
$result = file_get_contents ( $url , false, $context ); |
The code for http://www.nowamagic.net/test2.php is:
The result of the operation is:
5 |
[site] => www.nowamagic.net |
Some key points to explain:
1. The above procedure uses the Http_build_query () function, if you need to understand, you can see the PHP function completion: http_build_query () constructs a URL string.
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:
01 |
$context = stream_context_create( array ( |
04 |
‘header‘ => sprintf( "Authorization: Basic %s\r\n" , base64_encode ( $username . ‘:‘ . $password )). |
05 |
"Content-type: application/x-www-form-urlencoded\r\n" , |
06 |
‘content‘ => http_build_query( array ( ‘status‘ => $message )), |
10 |
$ret = file_get_contents ( ‘http://twitter.com/statuses/update.xml‘ , false, $context ); |
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:
08 |
$context = stream_context_create( $opts ); |
10 |
$html = file_get_contents ( ‘http://www.nowamagic.net‘ , false, $context ); |
12 |
//fopen输出文件指针处的所有剩余数据: |
13 |
//fpassthru($fp); //fclose()前使用 |
PHP Function Completion: stream_context_create () analog post/get