curl: (60) SSL certificate problem: unable to get local issuer certificate 错误

本文介绍了在使用PHP的cURL进行HTTPS请求时遇到的SSL证书问题:“无法获取本地颁发者证书”。提供了两种解决方案:一是禁用SSL验证;二是配置可信的CA证书路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天同事做微信分享时,碰到如下

SSL certificate problem: unable to get local issuer certificate。

的错误信息。

此问题的出现是由于没有配置信任的服务器HTTPS验证。默认,cURL被设为不信任任何CAs,就是说,它不信任任何服务器验证。

因此,这就是浏览器无法通过HTTPs访问你服务器的原因。

解决此报错有2种处理方法

  1.如果你的内容不敏感,一个快捷的方法是使用curl_exec()之前跳过ssl检查项。

  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

  2.下载一个ca-bundle.crt ,放到对应的目录,在php.ini文件中配置下路径

  https://github.com/bagder/ca-bundle/blob/e9175fec5d0c4d42de24ed6d84a06d504d5e5a09/ca-bundle.crt

  在php.ini加入 ,重启web服务器

curl.cainfo="真实路径/ca-bundle.crt"

注:项目使用的是laravel框架,在window电脑下,我首先查找的是如下链接:

http://stackoverflow.com/questions/24611640/curl-60-ssl-certificate-unable-to-get-local-issuer-certificate

解决办法如下:

证书在 https://curl.haxx.se/docs/caextract.html 链接里面。

关于“SSL证书问题:无法获取本地颁发者证书”错误。很明显,这适用于发送CURL请求的系统(并且没有服务器接收请求)

1)从https://curl.haxx.se/ca/cacert.pem下载最新的cacert.pem

2)将以下行添加到php.ini(如果这是共享托管,并且您无法访问php.ini,那么可以在public_html中添加到.user.ini)

curl.cainfo=/path/to/downloaded/cacert.pem

3)默认情况下,FastCGI进程将每隔300秒解析新文件(如果需要,可以通过添加几个文件来更改频率,如https://ss88.uk/blog/fast-cgi-and-user-ini -files-the-new-htaccess /


但是,下载的证书不可用,然后只好用

https://github.com/bagder/ca-bundle/blob/e9175fec5d0c4d42de24ed6d84a06d504d5e5a09/ca-bundle.crt

链接提供的证书解决了问题。

将下载的证书放在php.ini的当前目录下的extras/ssl/下面。

然后在php.ini文件中加入

curl.cainfo="真实路径/ca-bundle.crt"(curl扩展是必须开启的)


解决参考原文:

Thanks @Mladen Janjetovic,

Your suggestion worked for me in mac with ampps installed.

Copied: http://curl.haxx.se/ca/cacert.pem

To: /Applications/AMPPS/extra/etc/openssl/certs/cacert.pem

And updated php.ini with that path and restarted Apache:

[curl]
; A default value for the CURLOPT_CAINFO option. This is required to be an
; absolute path.
curl.cainfo="/Applications/AMPPS/extra/etc/openssl/certs/cacert.pem"
openssl.cafile="/Applications/AMPPS/extra/etc/openssl/certs/cacert.pem"

And applied same setting in windows AMPPS installation and it worked perfectly in it too.

[curl]
; A default value for the CURLOPT_CAINFO option. This is required to be an
; absolute path.
curl.cainfo="C:/Ampps/php/extras/ssl/cacert.pem"
openssl.cafile="C:/Ampps/php/extras/ssl/cacert.pem"

Same for wamp.

[curl]
; A default value for the CURLOPT_CAINFO option. This is required to be an
; absolute path.
curl.cainfo="C:/wamp/bin/php/php5.6.16/extras/ssl/cacert.pem"
openssl.cafile="C:/wamp/bin/php/php5.6.16/extras/ssl/cacert.pem"
share
 

When you view the http://curl.haxx.se/docs/caextract.html page, you will notice in big letters a section called:

RSA-1024 removed

Read it, then download the version of the certificates that includes the 'RSA-1024' certificates.https://github.com/bagder/ca-bundle/blob/e9175fec5d0c4d42de24ed6d84a06d504d5e5a09/ca-bundle.crt

Those will work with Mandrill.

Disabling SSL is a bad idea.





### Spring 应用中解决 cURL 错误 60 的方法 当遇到 `cURL error 60: SSL certificate problem: unable to get local issuer certificate` 错误时,这通常意味着客户端未能验证服务器的SSL证书。对于Spring应用程序而言,可以通过多种方式解决问题。 #### 方法一:安装并配置全局CA证书 为了使Java环境能够识别和信任特定的根证书颁发机构(CA),可以将受信的CA证书导入JVM的信任库中。具体操作如下: 1. 下载最新的 CA Bundle 文件(例如 cacert.pem),可从官方源获取[^4]。 2. 将该文件转换成适合 Java 使用的形式,即 JKS 或 PEM 格式的 keystore 文件。使用 keytool 工具完成这一过程: ```bash keytool -importcert -file /path/to/cacert.pem -alias my_ca_alias -keystore $JAVA_HOME/jre/lib/security/cacerts -storepass changeit ``` 3. 修改 JVM 启动参数以指向新的 trustStore 路径(如果需要的话)。可以在启动脚本里加入 `-Djavax.net.ssl.trustStore=/path/to/truststore.jks` 和 `-Djavax.net.ssl.trustStorePassword=yourpassword` 参数。 #### 方法二:禁用SSL验证(仅限测试) 虽然不推荐用于生产环境中,但在某些情况下可能希望暂时绕过SSL验证来进行调试或内部网络中的快速原型设计。可通过自定义 RestTemplate 来实现这一点: ```java @Bean public RestTemplate restTemplate() throws Exception { TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true; SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom() .loadTrustMaterial(null, acceptingTrustStrategy) .build(); SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext); CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(csf).build(); HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); requestFactory.setHttpClient(httpClient); return new RestTemplate(requestFactory); } ``` 这种方法会忽略所有的SSL认证失败情况,因此只应在绝对必要的情况下采用,并且绝不可部署于公开互联网上运行的服务之中。 #### 方法三:指定单个请求使用的CA证书 针对个别 HTTP 请求,也可以单独为其提供可信的 CA 证书链而无需更改整个系统的设置。下面是一个基于 Apache HttpClient 实现的例子: ```java // 加载PEM格式的CA证书作为输入流 InputStream caCertStream = getClass().getResourceAsStream("/path/to/your-ca-cert.pem"); KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); try (BufferedInputStream bis = new BufferedInputStream(caCertStream)) { CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509Certificate cert = (X509Certificate)cf.generateCertificate(bis); ks.load(null); // 初始化为空密钥存储区 ks.setCertificateEntry("ca", cert); } String algorithm = Security.getProperty("ssl.TrustManagerFactory.algorithm"); TrustManagerFactory tmf = TrustManagerFactory.getInstance(algorithm); tmf.init(ks); SSLContext ctx = SSLContext.getInstance("TLSv1.2"); ctx.init(null, tmf.getTrustManagers(), null); CloseableHttpClient client = HttpClients.custom() .setSSLContext(ctx) .build(); HttpGet httpGet = new HttpGet("https://example.com/"); HttpResponse response = client.execute(httpGet); System.out.println(response.getStatusLine()); client.close(); ``` 上述代码片段展示了如何创建一个带有预加载CA证书的信任管理器工厂实例,并将其应用于单一HTTP GET请求中。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

从心所愿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值