PHP使用file_get_contents发送http请求功能简单示例

201次阅读
没有评论

PHP 使用 file_get_contents 发送 http 请求功能

$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;

stream_context_create() 作用:创建并返回一个文本数据流并应用各种选项,可用于 fopen(),file_get_contents() 等过程的超时设置、代理服务器、请求方式、头信息设置的特殊过程。

正文完
有偿技术支持加微信
post-qrcode
 
评论(没有评论)
验证码