PHP 7.1:使用HTTP/2时无cURL [英] PHP 7.1: no cURL with HTTP/2

查看:625
本文介绍了PHP 7.1:使用HTTP/2时无cURL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题不是要确定服务器是否支持HTTP/2(两者都支持),而是如何确保PHP在CentOS中最新的PHP版本中使用具有正确cURL设置的HTTP/2(从Remi的存储库中构建).我自己的答案解决了一些与此有关的问题.

我已通过此精美指南将CentOS 7服务器设置为支持HTTP/2:在您的网络服务器上设置HTTP/2

I've setup my CentOS 7 server to support HTTP/2 with the help of this fine guide: Setting up HTTP/2 on your web server

因此,我已经有了最新版本,对其进行了编译和安装,并且一切正常.网页完全按照其应有的HTTP/2标头提供服务.

So I've got the latest releases, compiled and installed them and all is working fine. Pages are served with HTTP/2 headers, exactly as they should.

现在,我想在我的PHP脚本中使用HTTP/2功能.主要是通过以HTTP/2方式进行cURL调用.我发现我需要升级libcurl并使用特定的HTTP/2支持对其进行编译.遇到一些挫折,我开始努力了.

Now I'd like to utilize HTTP/2 functionality in my PHP scripts. Mainly by doing cURL calls in HTTP/2 fashion. I found out I needed to upgrade libcurl and build and compile it with specific HTTP/2 support. With some setbacks, I got it to work.

所以curl -V告诉我:

curl 7.55.0-DEV (x86_64-unknown-linux-gnu) libcurl/7.55.0-DEV OpenSSL/1.1.0f zlib/1.2.7 nghttp2/1.23.1
Release-Date: [unreleased]
Protocols: dict file ftp ftps gopher http https imap imaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp 
Features: IPv6 Largefile NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets HTTPS-proxy 

太好了!

phpinfo()和新安装的OpenSSL一起为我提供了相同的版本(7.55.0-DEV),

And phpinfo() gives me the same version (7.55.0-DEV) in conjunction with the newly installed OpenSSL, which is:

OpenSSL 1.1.0f 25 May 2017

还有Apache,顺便说一句:

And Apache, BTW:

Server version: Apache/2.4.26 (Unix)
Server built:   Jul  7 2017 09:47:22

但是,当我编写一个带有cURL调用并将CURLOPT_HTTP_VERSION选项设置为CURL_HTTP_VERSION_2_0的简单PHP脚本时,如本示例中所示:https://stackoverflow.com/a/37146586/1005334 ,它告诉我服务器不支持HTTP/2 ...

However, when I write a simple PHP script with a cURL call and the CURLOPT_HTTP_VERSION option set to CURL_HTTP_VERSION_2_0, as outlined in this example: https://stackoverflow.com/a/37146586/1005334, it tells me the server does not have HTTP/2 support...

怎么可能?

我注意到的一件事是,尽管phpinfo()中提到的OpenSSL版本声明了OpenSSL 1.1.0f,但phpinfo()中有关OpenSSL模块的内容仍然提到了我的旧OpenSSL版本(OpenSSL 1.0.1e-fips).

One thing I noticed, is the bit about the OpenSSL module in phpinfo() still mentions my old OpenSSL version (OpenSSL 1.0.1e-fips), although the OpenSSL-version mentioned with the cURL module states OpenSSL 1.1.0f.

我已经从Remi的存储库中安装了PHP 7.1(当前版本为7.1.7),可能是它是针对较旧的OpenSSL版本构建的,因此无法与我的较新版本一起使用吗?我还阅读了PHP 7中对HTTP/2的支持不是很明显,但是我想这应该不是问题,对吧?

I've installed PHP 7.1 (it currently stands at version 7.1.7) from Remi's repository, could it be it is built against an older OpenSSL version so it will not work with my newer one? I also read support in PHP 7 for HTTP/2 is not quite apparent, but I imagine it shouldn't be a problem, right?

还是不是OpenSSL的问题,我应该在其他地方看看,也许从源代码编译PHP并带有一些预期的标志?

Or is OpenSSL not the problem and should I look elsewhere, perhaps compiling PHP from source with some intended flags?

推荐答案

经过一番摆弄之后,看来我可以通过带有Remi的PHP包的PHP cURL调用获得HTTP/2响应,并且获得了新鲜的体验.内置的cURL/OpenSSL.

After some fiddling, it seems I can get a HTTP/2 response through a PHP cURL call with Remi's PHP packages and a freshly built cURL/OpenSSL.

我正在使用这段代码进行测试(摘自此处):

I was using this piece of code to test (taken from here):

if (curl_version()["features"] & CURL_VERSION_HTTP2 !== 0) {
    $url = "https://www.google.com/";
    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_URL            =>$url,
        CURLOPT_HEADER         =>true,
        CURLOPT_NOBODY         =>true,
        CURLOPT_RETURNTRANSFER =>true,
        CURLOPT_HTTP_VERSION   =>CURL_HTTP_VERSION_2_0,
    ]);
    $response = curl_exec($ch);
    if ($response !== false && strpos($response, "HTTP/2.0") === 0) {
        echo "HTTP/2 support!";
    } elseif ($response !== false) {
        echo "No HTTP/2 support on server.";
    } else {
        echo curl_error($ch);
    }
    curl_close($ch);
} else {
    echo "No HTTP/2 support on client.";
}

所需的调整是:

CURLOPT_HTTP_VERSION => 3

...并且:

if ($response !== false && strpos($response, "HTTP/2") === 0)

因此,我使用的是3,而不是cURL选项中的HTTP版本的CURL_HTTP_VERSION_2_0,这应该是相同的意思,但是显然,当使用CURL_HTTP_VERSION_2_0时,PHP无法获得正确的值.

So instead of CURL_HTTP_VERSION_2_0 as HTTP version in the cURL options, I used 3, which should mean the same, but apparently PHP does not pick up the correct value when using CURL_HTTP_VERSION_2_0.

看着它给出的原始响应,我注意到Google正在发回HTTP/2而不是HTTP/2.0.

Looking at the raw response this gave, I noticed Google was sending back HTTP/2 instead of HTTP/2.0.

我希望这可以帮助某人!

I hope this can help someone!

这篇关于PHP 7.1:使用HTTP/2时无cURL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆