PHP 设置自定义 header 以“:”开头

PHP Setting custom header starting with ':'

我需要设置一些以“:”开头的自定义 header。

$option['headers'][] = ":authority: example.com"; //<-- Here is the problem
$option['headers'][] = "accept-encoding: gzip, deflate, br";
$option['post'] = json_encode(array("Domain"=>"example.com"));
$url = "https://www.google.com";
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36");
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_COOKIEFILE,"file.cookie");
curl_setopt($ch,CURLOPT_COOKIEJAR,"file.cookie");
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_VERBOSE, true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $option['post']);
curl_setopt($ch, CURLOPT_HTTPHEADER, $option['headers']);
$getdata = curl_exec($ch);

我尝试用 chr(58) 替换“:”,但同样的问题。我收到错误 55 并从日志“* 发送 HTTP POST 请求失败”。如果我评论第一行是有效的,但我真的需要 header。我被困在这里了。有什么解决办法吗?

:authority: 看起来像一个 HTTP/2 伪 header,你不能用 curl 像这样设置它们。然而,curl 会自行传递它,并且它将使用与为 Host: 设置的内容相同的内容以使其以相同的方式工作,而与最终使用的 HTTP 版本无关(它也将与 HTTP/3).