手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表2009年02月13日的文章

通过file_get_contents来Post数据的实例

用file_get_contents来进行POST?很妖吧。
我也没有想到还有这种妖的东西。在向东的博客上看到这个的:http://www.xiangdong.org/blog/post/1623/

回忆未来?别惊讶,用他的话来说是山寨D。

原文:

file_get_contents.php: Post数据

PHP代码
  1. <?php  
  2. function Post($url$post = null)  
  3. {  
  4.     $context = array();  
  5.   
  6.     if (is_array($post))  
  7.     {  
  8.         ksort($post);  
  9.   
  10.         $context['http'] = array  
  11.         (  
  12.             'method' => 'POST',  
  13.             'content' => http_build_query($post'''&'),  
  14.         );  
  15.     }  
  16.   
  17.     return file_get_contents($url, false, stream_context_create($context));  
  18. }  
  19.   
  20. $data = array  
  21. (  
  22.     'name' => 'test',  
  23.     'email' => 'test@gmail.com',  
  24.     'submit' => 'submit',  
  25. );  
  26.   
  27. echo Post('http://localhost/5-5/request_post_result.php'$data);  
  28. ?>  
接收数据:

request_post_result.php  接收经过Post的数据:
PHP代码
  1. <?php  
  2. echo $_POST['name'];  
  3. echo $_POST['email'];  
  4. echo $_POST['submit'];  
  5. echo "fdfd";  
  6. ?>  


看一下,里面有一个特别的函数:stream_context_create,翻开手册看了一下,也没说什么呀,只是说:Creates and returns a stream context with any options supplied in options preset.

而file_get_contents呢?它说:

Note: Context support was added with PHP 5.0.0. For a description of contexts, refer to Reference CLX, Stream Functions.

关于Stream Functions,手册上这么描述的。。。

A wrapper is additional code which tells the stream how to handle specific protocols/encodings. For example, the http wrapper knows how to translate a URL into an HTTP/1.0 request for a file on a remote server. There are many wrappers built into PHP by default (See Appendix O), and additional, custom wrappers may be added either within a PHP script using stream_wrapper_register(), or directly from an extension using the API Reference in Chapter 52. Because any variety of wrapper may be added to PHP, there is no set limit on what can be done with them. To access the list of currently registered wrappers, use stream_get_wrappers().

A stream is referenced as: scheme://target

  • scheme(string) - The name of the wrapper to be used. Examples include: file, http, https, ftp, ftps, compress.zlib, compress.bz2, and php. See Appendix O for a list of PHP built-in wrappers. If no wrapper is specified, the function default is used (typically file://).

  • target - Depends on the wrapper used. For filesystem related streams this is typically a path and filename of the desired file. For network related streams this is typically a hostname, often with a path appended. Again, see Appendix O for a description of targets for built-in streams.

Tags: php, post

MySQL优化指导建议[不知道能不能发,所以加密]

这篇日志被加密了,请输入密码后查看。

公交见闻

昨天上班、下班乘公车发现有趣的事情还是挺多。人生都这样吧,大概。

上班的时候,乘856,有位仁兄和电子报站器同步播音。开始的时候我被吓了一跳,电子报站器怎么破音了?后来才发现是这位仁兄在辛苦的为公车乘客们提供方便。

晚上下班的时候,看到两个黄皮肤,黑眼睛的人在聊天,聊什么我没听懂,他们很投入,我也不能打断别人是吧。这一男一女兴致极高,知道我听不懂他们说的洋文,然后就很投入的在说。
那位男青年,说的是很流畅,女青年则非常生疏,几乎就是一个单词一个单词的往外蹦。很是乐此不彼。

感慨不算多。

顺便,听说户籍制度要改革了,但好象我并在制度范围内,可怜的。。。

Tags: 公交