PHP curl 的 SSL 错误 [英] SSL error with PHP curl

查看:39
本文介绍了PHP curl 的 SSL 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各位程序员好,

我正在制作一个带有 https 连接的 API.我想通过证书检查来保护 API 调用.现在我已经从 curl 下载了 cacert.pem 并将其放在我的文件夹中,但它不起作用并给我这个错误:

I'm making an API with a https connection. I want to secure the API call with a certificate check. Now i've downloaded the cacert.pem from curl and placed this in my folder, but it doesn't work and gives me this error:

SSL certificate problem: unable to get local issuer certificate

嗯,我真的很确定 cacert.pem 文件的位置正确,但是对于你们来说,我一直在编写一些代码来使其工作:

Well i'm really sure that the cacert.pem file is located correctly but for you guys some code that i've been writing to get it working:

 $ch = curl_init($url);
 curl_setopt($ch, CURLOPT_POST      ,1);
 curl_setopt($ch, CURLOPT_USERPWD, $this->api_key . ":" . $this->api_secret);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
 curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . "/cacert.pem");
 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
 curl_setopt($ch, CURLOPT_POSTFIELDS    ,$post_vars);
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION  ,0);
 curl_setopt($ch,CURLOPT_HTTPHEADER,array('Accept: application/json'));
 curl_setopt($ch, CURLOPT_RETURNTRANSFER  ,1);  // RETURN THE CONTENTS OF THE CALL
 $data = curl_exec($ch);

我希望你们中的一些人能回答我的问题,因为我真的不太了解 SSL 连接.

I hope you some of you guys have an answer for my question because I really don't know much about SSL connections.

提前致谢!

推荐答案

在确保您正在运行的脚本可以读取您的证书包后,下一步是调查服务器本身.

After having made sure that your certificate bundle is readable by the script you're running, the next step is to investigate the server itself.

如果这是您自己的服务器,则您可能忘记安装中间证书,即介于已颁发的证书和捆绑包附带的证书颁发机构之一之间的证书.

If this is your own server, it's likely that you forgot to install the intermediate certificates, i.e. the ones in between the issued certificate and one of the certificate authorities that comes with the bundle.

如果您使用的是自签名证书,则您的 cacert.pem 应包含您正在连接的服务器的公共证书;这可以从私有证书生成,在此处概述,称为.crt 文件.

If you're working with a self-signed certificate, your cacert.pem should contain the public certificate of the server you're connecting to; this can be generated from the private certificate, outlined here, referred to as the .crt file.

这篇关于PHP curl 的 SSL 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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