PHP function Completion: stream_context_create () Analog post/get

Source: Internet
Author: User

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:

01 $dataarray(
02     ‘foo‘=>‘bar‘,
03     ‘baz‘=>‘boom‘,
04     ‘site‘=>‘www.nowamagic.net‘,
05     ‘name‘=>‘nowa magic‘);
06     
07 $data= http_build_query($data);
08
09 //$postdata = http_build_query($data);
10 $optionsarray(
11     ‘http‘=> array(
12         ‘method‘=> ‘POST‘,
13         ‘header‘=> ‘Content-type:application/x-www-form-urlencoded‘,
14         ‘content‘=> $data
15         //‘timeout‘ => 60 * 60 // 超时时间(单位:s)
16     )
17 );
18
19 $url"http://www.nowamagic.net/test2.php";
20 $context= stream_context_create($options);
21 $resultfile_get_contents($url, false, $context);
22
23 echo$result;

The code for http://www.nowamagic.net/test2.php is:

1 $data$_POST;
2
3 echo‘<pre>‘;
4 print_r( $data);
5 echo‘</pre>‘;

The result of the operation is:

1 Array
2 (
3     [foo] => bar
4     [baz] => boom
5     [site] => www.nowamagic.net
6     [name] => nowa magic
7 )

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(
02     ‘http‘=> array(
03         ‘method‘=> ‘POST‘,
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)),
07         ‘timeout‘=> 5,
08     ),
09 ));
10 $retfile_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:

01 $optsarray(
02     ‘http‘=>array(
03     ‘method‘=>"GET",
04     ‘timeout‘=>60,
05   )
06 );
07 //创建数据流上下文
08 $context= stream_context_create($opts);
09
10 $html=file_get_contents(‘http://www.nowamagic.net‘, false, $context);
11
12 //fopen输出文件指针处的所有剩余数据:
13 //fpassthru($fp); //fclose()前使用

PHP Function Completion: stream_context_create () analog post/get

Related Article

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.