这篇文章主要介绍的内容是关于php利用file_get_contents发送http请求,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
服务器端模拟 post/get 等请求,使用 curl 很容易办到,那么如果不使用 curl 库,又该怎么办呢?
$data = array(
'test'=>'bar',
'baz'=>'boom',
'site'=>'www.nimip.com',
'name'=>'nimip.com');
$data = http_build_query($data);
//$postdata = http_build_query($data);
$options = array(
'http' => array(
'method' => 'post',
'header' => 'content-type:application/x-www-form-urlencoded',
'content' => $data
'timeout' => 60 // 超时时间(单位:s)
)
);
$url = "http://www.nimip.com";
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo $result;
http://www.nimip.com的代码为:
$data = $_post;
print_r( $data );
stream_context_create() 作用:创建并返回一个文本数据流并应用各种选项,可用于fopen(),file_get_contents()等过程的超时设置、代理服务器、请求方式、头信息设置的特殊过程。
相关推荐:
php利用qqwry.bat获取实际使用地址
如何解决php用file_get_contents方法抓取网页数据出现乱码
以上就是php利用file_get_contents发送http请求的详细内容。