这次给大家带来PHP使用file_get_contents发送http请求步骤详解,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.testweb.com";
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo $result;其中http://www.testweb.com的代码为:
$data = $_POST; print_r( $data );
stream_context_create() 作用:创建并返回一个文本数据流并应用各种选项,可用于<a href="http://www.php.cn/wiki/1325.html" target="_blank">fopen</a>(),file_get_contents()等过程的超时设置、代理服务器、请求方式、头信息设置的特殊过程。
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
以上就是PHP使用file_get_contents发送http请求步骤详解的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号