«

解决SSL证书验证问题的方法(cURL error 60: SSL certificate problem: unable to get local issuer certificate)

chunkuan 发布于 阅读:445 后端


$url = 'https://www.xxx.com/aaa.htm';
        echo (Http::get($url)->body());

报错:

GuzzleHttp\Exception\RequestException
cURL error 60: SSL certificate problem: unable to get local issuer certificate (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://gjhz.sxpi.edu.cn/lhlx.htm

这个错误是因为 cURL 无法验证目标网站的 SSL 证书,因为缺少本地颁发机构证书(CA 证书)。这是一个常见的安全验证问题。

有几种解决方法:

方法 1:禁用 SSL 验证(不推荐用于生产环境)

$url = 'https://www.xxx.com/aaa.htm';
echo Http::withOptions(['verify' => false])->get($url)->body();

方法 2:指定正确的 CA 证书路径(推荐)

  1. 下载最新的 CA 证书包,例如从 https://curl.se/docs/caextract.html 下载 cacert.pem
  2. 将其保存到你的项目目录中
  3. 在代码中指定证书路径:
$url = 'https://www.xxx.com/aaa.htm';
echo Http::withOptions(['verify' => '/path/to/cacert.pem'])->get($url)->body();

方法 3:更新系统的 CA 证书

如果你有服务器管理权限,可以更新系统的 CA 证书:

方法 4:如果是 Laravel 的 HTTP 客户端

Laravel 8+ 的 HTTP 客户端默认使用 Guzzle,你可以这样配置:

$url = 'https://www.xxx.com/aaa.htm';
echo Http::withOptions([
    'verify' => true, // 或指定证书路径
])->get($url)->body();

建议使用方法 2,因为它保持了安全性。只在开发和测试环境中使用方法 1。

ssl cURL RequestException CA 证书