PHP Curl - 不允许时收到 HTTP/0.9 [英] PHP Curl - Received HTTP/0.9 when not allowed
问题描述
当我尝试向苹果推送服务发送 HTTP/2.0 后请求时,我偶然发现了一个奇怪的行为:
I stumbled over a weird behavior when I try to send a post HTTP/2.0 request to apples push service:
$http2ch = curl_init();
curl_setopt($http2ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
curl_setopt($http2ch, CURLOPT_URL, 'https://api.push.apple.com/3/device/megauniquedevicetokendummy');
curl_setopt($http2ch, CURLOPT_PORT, 443);
curl_setopt($http2ch, CURLOPT_HTTPHEADER, $httpHeader);
curl_setopt($http2ch, CURLOPT_POST, true);
curl_setopt($http2ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($http2ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($http2ch, CURLOPT_TIMEOUT, 30);
curl_setopt($http2ch, CURLOPT_HEADER, 1);
$result = curl_exec($http2ch);
if ($result === false) {
throw new \Exception("Curl failed: " . curl_error($http2ch) . " | " . curl_getinfo($http2ch, CURLINFO_HTTP_CODE));
}
异常与消息一起抛出:卷曲失败:在不允许的情况下接收到 HTTP/0.9 |0
The exception is thrown with the Message: Curl failed: Received HTTP/0.9 when not allowed | 0
我在上面截取的代码的第二行明确告诉 curl 使用 HTTP/2.0.有谁知道该错误消息的含义以及为什么 curl 使用如此旧的 HTTP 版本?
I explicitly told curl to use HTTP/2.0 on the second line of the code snipped above. Does anyone have any idea what that error message means and why curl uses such an old HTTP version?
我使用的是 PHP 7.2 和 curl 版本 7.66.0.
I am on PHP 7.2 and curl version 7.66.0.
推荐答案
我想通了.确保使用 nghttp2 编译 curl.
I figured it out. Make sure that curl is compiled with nghttp2.
如果您不确定,可以在终端上使用 curl --version
If you are unsure, you can check it on your terminal using curl --version
如果没有找到 nghttp2/{version} 则需要用 nghttp2 重新编译 curl.
If you dont find nghttp2/{version} you need to compile curl again with nghttp2.
curl --version
缺少 nghttp2 的示例:
curl --version
example where nghttp2 is missing:
curl 7.66.0 (amd64-portbld-freebsd12.0) libcurl/7.66.0 OpenSSL/1.1.1d zlib/1.2.11
curl --version
nghttp2 可用的示例:
curl --version
example where nghttp2 is available:
curl 7.64.1 (x86_64-apple-darwin19.0) libcurl/7.64.1 (SecureTransport) LibreSSL/2.8.3 zlib/1.2.11 nghttp2/1.39.2
这篇关于PHP Curl - 不允许时收到 HTTP/0.9的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!