Loading

PHP利用file_get_contents发送http请求

记录一篇转载的文章

使用file_get_contents发送http请求,
这样可以在没有安装curl扩展的老机器上使用HTTP调用

$data = [
    'test' => 'bar',
    'baz' => 'foo',
    'site' => 'www.nimip.com',
    'name' => 'nimip.com'
];

$options = [
    'http' => [
        'method' => 'POST',
        'header' => ['Content-type:application/json', "Cookie: foo=bar"],
        'content' => json_encode($data),
        'timeout' => 30 // 超时时间(单位:s)
    ]
];

$url = "http://www.example.com/";
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);

echo $result;

posted @ 2021-01-20 10:37  chnyang  阅读(242)  评论(0编辑  收藏  举报