CentOS 7 如何更新通过 yum 安装的 PHP 带的 curl 扩展??

1. 运行环境

1). 当前使用的各软件版本?

  • CentOS 7
  • PHP 7.4.30
  • curl 7.85.0(目前最新版本)

2. 问题描述?

项目是用 Laravel 写的爬虫,运行在 CLI 模式,需要使用 HTTP2.0 协议。
CentOS 自带的 curl 版本比较老,不支持 HTTP2.0
导致代码报错 Undefined constant 'CURL_HTTP_VERSION_2_0'
尝试用 Yum 更新 curl 后,直接用命令行是可以用 HTTP2.0 协议,但是 PHP 依然不行。

期望的结果

PHP 代码也能正常使用 curl 发出 HTTP2.0 协议的请求。

尝试过的方法

通过 yum 升级 libcurl 后,使用命令验证,可以看到已经支持 HTTP2.0 了。

curl -V
curl 7.85.0 (x86_64-redhat-linux-gnu) libcurl/7.85.0 NSS/3.53.1 zlib/1.2.7 libpsl/0.20.2 (+libidn2/2.3.2) libssh2/1.10.0 nghttp2/1.33.0
Release-Date: 2022-08-31
Protocols: dict file ftp ftps gopher gophers http https imap imaps ldap ldaps mqtt pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp 
Features: alt-svc AsynchDNS GSS-API HSTS HTTP2 HTTPS-proxy IPv6 Kerberos Largefile libz MultiSSL NTLM NTLM_WB PSL SPNEGO SSL UnixSockets

实际请求一下也是支持的。

curl --http2 -I https://nghttp2.org
HTTP/2 200 
date: xxx, xx Sep 2022 xx:xx:xx GMT
content-type: text/html
last-modified: xxx, xx xxx 2022 xx:xx:xx GMT
etag: "xxxxxbb0-18b4"
accept-ranges: bytes
content-length: 6324
x-backend-header-rtt: 0.006897
strict-transport-security: max-age=31536000
server: nghttpx
alt-svc: h3=":443"; ma=3600, h3-29=":443"; ma=3600
via: 2 nghttpx
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
x-content-type-options: nosniff

不过 phpinfo() 虽然显示了正确的版本,却依然没有 HTTP2.0,与此对比,Homestead 里面的 PHP 就有 HTTP2 => Yes
下面是 CentOS 7 里面的 php -i

curl

cURL support => enabled
cURL Information => 7.85.0
Age => 9
Features
AsynchDNS => Yes
CharConv => No
Debug => No
GSS-Negotiate => No
IDN => No
IPv6 => Yes
krb4 => No
Largefile => Yes
libz => Yes
NTLM => Yes
NTLMWB => Yes
SPNEGO => Yes
SSL => Yes
SSPI => No
TLS-SRP => No
Protocols => dict, file, ftp, ftps, gopher, gophers, http, https, imap, imaps, ldap, ldaps, mqtt, pop3, pop3s, rtsp, scp, sftp, smb, smbs, smtp, smtps, telnet, tftp
Host => x86_64-redhat-linux-gnu
SSL Version => NSS/3.53.1
ZLib Version => 1.2.7
libSSH Version => libssh2/1.10.0

Directive => Local Value => Master Value
curl.cainfo => no value => no value

推测是因为 PHP 的 curl.so 扩展没有更新的原因,但是这台服务器的 PHP 是通过 yum 安装的 remi 源多版本共存,不知道应该怎么更新 curl.so 扩展。大佬们有没有办法更新这个扩展呢?

提前感谢各位大佬赐教。

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
最佳答案

重新编译 php 的 curl 扩展不就行了?

1年前 评论
tomcath (楼主) 1年前
讨论数量: 8
陈先生

我记得 curl 要声明 version, 具体你可以看下 guzzle 的代码

1年前 评论
tomcath (楼主) 1年前
陈先生 (作者) 1年前
if (
    defined("CURL_VERSION_HTTP2") &&
    (curl_version()["features"] & CURL_VERSION_HTTP2) !== 0
) {
    $url = "https://www.bilibili.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) {
        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.";
}

我是 CentOS Linux release 8.2.2004 (Core) php代码的执行结果

HTTP/2 support!
1年前 评论
tomcath (楼主) 1年前
yyy123456 (作者) 1年前

重新编译 php 的 curl 扩展不就行了?

1年前 评论
tomcath (楼主) 1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!