【转】通过file_get_contents来Post数据的实例 BY 膘叔
2011-04-27 20:56
381 查看
用file_get_contents来进行POST?很妖吧。
我也没有想到还有这种妖的东西。在向东的博客上看到这个的:http://www.xiangdong.org/blog/post/1623/
回忆未来?别惊讶,用他的话来说是山寨D。
原文:
file_get_contents.php: Post数据
PHP代码
<?php
function Post($url, $post = null)
{
$context = array();
if (is_array($post))
{
ksort($post);
$context['http'] = array
(
'method' => 'POST',
'content' => http_build_query($post, '', '&'),
);
}
return file_get_contents($url, false, stream_context_create($context));
}
$data = array
(
'name' => 'test',
'email' => 'test@gmail.com',
'submit' => 'submit',
);
echo Post('http://localhost/5-5/request_post_result.php', $data);
?>
接收数据:
request_post_result.php 接收经过Post的数据:
PHP代码
<?php
echo $_POST['name'];
echo $_POST['email'];
echo $_POST['submit'];
echo "fdfd";
?>
看一下,里面有一个特别的函数:stream_context_create,翻开手册看了一下,也没说什么呀,只是说:Creates and returns a stream context with any options supplied in
而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:
原文地址:http://www.neatstudio.com/show-503-1.shtml
我也没有想到还有这种妖的东西。在向东的博客上看到这个的:http://www.xiangdong.org/blog/post/1623/
回忆未来?别惊讶,用他的话来说是山寨D。
原文:
file_get_contents.php: Post数据
PHP代码
<?php
function Post($url, $post = null)
{
$context = array();
if (is_array($post))
{
ksort($post);
$context['http'] = array
(
'method' => 'POST',
'content' => http_build_query($post, '', '&'),
);
}
return file_get_contents($url, false, stream_context_create($context));
}
$data = array
(
'name' => 'test',
'email' => 'test@gmail.com',
'submit' => 'submit',
);
echo Post('http://localhost/5-5/request_post_result.php', $data);
?>
接收数据:
request_post_result.php 接收经过Post的数据:
PHP代码
<?php
echo $_POST['name'];
echo $_POST['email'];
echo $_POST['submit'];
echo "fdfd";
?>
看一下,里面有一个特别的函数:stream_context_create,翻开手册看了一下,也没说什么呀,只是说:Creates and returns a stream context with any options supplied in
optionspreset.
而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.
原文地址:http://www.neatstudio.com/show-503-1.shtml
相关文章推荐
- 通过file_get_contents来Post数据的实例
- 通过Curl、socket、file_get_contents三种方法使用POST提交数据
- php通过curl post和get发送json数据实例 curl命令转为php源码
- 利用Curl、socket、file_get_contents POST数据
- file_get_contents post数据获取
- 使用Curl、socket、file_get_contents三种方法POST提交数据
- php中使用Curl、socket、file_get_contents三种方法POST提交数据
- 使用Curl、socket、file_get_contents三种方法POST提交数据
- 使用Curl、socket、file_get_contents三种方法POST提交数据
- 利用Curl、socket、file_get_contents POST数据
- file_get_contents 模拟POST数据发送
- PHP利用Curl、socket、file_get_contents POST数据
- php中使用Curl、socket、file_get_contents三种方法POST提交数据
- php中使用Curl、socket、file_get_contents三种方法POST提交数据
- file_get_contents获取一个页面并发送 POST 数据
- 使用Curl、socket、file_get_contents三种方法POST提交数据
- PHP中使用file_get_contents post数据代码例子
- PHP中使用file_get_contents post数据代码例子
- php中使用Curl、socket、file_get_contents三种方法POST提交数据
- PHP利用Curl、socket、file_get_contents POST数据