PHP Curl - 不允许时收到 HTTP/0.9

roc*_*ckZ 4 php curl http apple-push-notifications

当我尝试向苹果推送服务发送 HTTP/2.0 后请求时,我偶然发现了一个奇怪的行为:

        $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));
       }
Run Code Online (Sandbox Code Playgroud)

抛出异常并显示消息:Curl failed: Received HTTP/0.9 when not allowed | 0

我明确告诉 curl 在上面截取的代码的第二行中使用 HTTP/2.0。有谁知道该错误消息的含义以及为什么 curl 使用如此旧的 HTTP 版本?

我使用的是 PHP 7.2 和 curl 版本 7.66.0。

roc*_*ckZ 9

我想到了。确保curl是使用nghttp2编译的。

如果您不确定,您可以使用以下命令在终端上检查curl --version

如果没有找到 nghttp2/{version},则需要使用 nghttp2 再次编译curl。

curl --version缺少 nghttp2 的示例:

curl 7.66.0 (amd64-portbld-freebsd12.0) libcurl/7.66.0 OpenSSL/1.1.1d zlib/1.2.11
Run Code Online (Sandbox Code Playgroud)

curl --versionnghttp2 可用的示例:

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
Run Code Online (Sandbox Code Playgroud)

  • 即使使用 nghttp2 编译的curl 也有问题。 (11认同)
  • 同样在这里。我的curl(7.79.1)有nghttp2/1.45.1并给出HTTP/0.9错误。我正在运行最新的 nghttpd Web 服务器进行测试。 (2认同)

小智 5

当服务器是 grpc 服务器时,也会发生这种情况。当 curl 对 grpc 服务器或其他未响应 curl 期望的有效 HTTP 状态行的非 HTTP 服务器运行时,curl 将打印“收到的 HTTP/0.9 不允许时”。

如果 curl 打印诸如“未知协议”之类的内容而不是假设它是 0.9 可能会更好,因为如今访问诸如 grpc 服务器之类的内容将比实际的 HTTP 0.9 服务器更常见。

  • 那么,有什么解决办法呢?其他人不适合我。 (2认同)
  • 我确认针对 Samba/SMB (Windows) 服务器运行的“curl http://<server_ip:139>”也会显示此消息。 (2认同)