PHP 的 curl 在执行请求时似乎不包含 Host 标头:
<?php
$handle = curl_init('http://example.com/');
curl_setopt_array($handle, array(
CURLOPT_USERAGENT => 'test ua',
CURLOPT_REFERER => 'http://example.org/',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_PROXY => '127.0.0.1:8080',
CURLOPT_PROXYTYPE => CURLPROXY_HTTP,
//Doesn't work if you specify it manually or leave it out:
CURLOPT_HTTPHEADER => array(
'Host: example.com'
)
));
print_r(curl_getinfo($handle));
echo curl_exec($handle);
?>
主机标头由 curl_init() 的参数定义
当您指定请求 URL 时,PHP 已经自动使用 Host
标头。
以下示例应足以确认:
<?php
$handle = curl_init('http://pgl.yoyo.org/http/browser-headers.php');
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($handle);
?>
输出 HTML 列出了请求标头,其中我们看到 Host: pgl.yoyo.org
,正如预期的那样。
这是一个完全不相关的错误,它是通过在用户代理内部使用换行符而发生的,例如:
curl_setopt_array($handle, array(
CURLOPT_USERAGENT => "user-agent'n"
));