可以使用 stream_context_create 函数创建一个包含请求头的上下文,然后在调用 file_get_contents 函数时将其作为第三个参数传入。

示例代码:

$url = 'http://example.com';
$header = array(
    'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
);
$options = array(
    'http' => array(
        'header' => $header
    )
);
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);

echo $response;

在上面的例子中,我们使用了一个伪造的 User-Agent 请求头。在 $options 数组中,我们创建了一个包含请求头的上下文,并将其传递给 file_get_contents 函数。最后,我们输出了响应

php file_get_contents 带上请求头发起请求

原文地址: https://www.cveoy.top/t/topic/crml 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录