手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆

通过file_get_contents来Post数据的实例

首页 > PHP >

用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

« 上一篇 | 下一篇 »

只显示10条记录相关文章

使用PHP得到所有的HTTP请求头 (浏览: 63308, 评论: 3)
我为什么会选用phpstorm (浏览: 53311, 评论: 5)
快速生成目录树 (浏览: 47348, 评论: 7)
PHP导入导出Excel方法 (浏览: 45815, 评论: 3)
PHP的XSS攻击过滤函数 (浏览: 43305, 评论: 2)
PHP中Eval的作用 (浏览: 42179, 评论: 4)
超详细:在Mac OS X中配置Apache + PHP + MySQL (浏览: 41364, 评论: 1)
PHP常见错误(二) (浏览: 40423, 评论: 1)
PHP sendmail (浏览: 38465, 评论: 7)
几个ZendStudio使用教程 (浏览: 37036, 评论: 0)

5条记录访客评论

非常棒。。。不错。。。可以搞破坏

Post by xx on 2012, June 15, 3:33 PM 引用此文发表评论 #1

Thanks.

Post by kevin on 2010, August 17, 5:06 PM 引用此文发表评论 #2

呵呵,网上搜索 “ php POST 数据 ”,发现排在第一的是你哈

Post by 文福 on 2010, July 15, 3:08 PM 引用此文发表评论 #3

好贴,学习了

Post by 好贴 on 2010, July 12, 10:06 AM 引用此文发表评论 #4

前来围观!

Post by 爆波团队长 on 2010, May 17, 10:19 PM 引用此文发表评论 #5


发表评论

评论内容 (必填):