curl_setopt换成 curl_easy_setopt  cacert.pem路径换成字符串 CURLOPT_SSL_VERIFYHOST 解决error 51 大概7.28版本后要设置2,不是true

今天一个同事反映,使用curl发起https请求的时候报错:“SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed”

很明显,验证证书的时候出现了问题。

使用curl如果想发起的https请求正常的话有2种做法:

方法一、设定为不验证证书和host。

在执行curl_exec()之前。设置option

$ch = curl_init();

......

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

方法二、设定一个正确的证书。

本地ssl判别证书太旧,导致链接报错ssl证书不正确。

我们需要下载新的ssl 本地判别文件

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

放到 程序文件目录

curl 增加下面的配置

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,true); ;
   curl_setopt($ch,CURLOPT_CAINFO,dirname(__FILE__).'/cacert.pem');

大功告成

(本人验证未通过。。。报错信息为:SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed)

如果对此感兴趣的话可以参看国外一大神文章。http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/

为了防止某天该文章被Q今复制过来。内容如下:

Using cURL in PHP to access HTTPS (SSL/TLS) protected sites

From PHP, you can access the useful cURL Library (libcurl) to make requests to URLs using a variety of protocols such as HTTP, FTP, LDAP and evenGopher. (If you’ve spent time on the *nix command line, most environments also have the curl command available that uses the libcurl library)

In practice, however, the most commonly-used protocol tends to be HTTP, especially when usingPHP for server-to-server communication. Typically this involves accessing another web server as part of a web service call, using some method such asXML-RPC or REST to query a resource. For example, Delicious offers a HTTP-based API to manipulate and read a user’s posts. However, when trying to access a HTTPS resource (such as the delicious API), there’s a little more configuration you have to do before you can get cURL working right in PHP.

The problem

If you simply try to access a HTTPS (SSL or TLS-protected resource) in PHP using cURL, you’re likely to run into some difficulty. Say you have the following code: (Error handling omitted for brevity)

// Initialize session and set URL.
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url);
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);

If $url points toward an HTTPS resource, you’re likely to encounter an error like the one below:

Failed: Error Number: 60. Reason: SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

The problem is that cURL has not been configured to trust the server’s HTTPS certificate. The concepts of certificates and PKI revolves around the trust of Certificate Authorities (CAs), and by default, cURL is setup to not trust any CAs, thus it won’t trust any web server’s certificate. So why don’t you have problems visiting HTTPs sites through your web browser? As it happens, the browser developers were nice enough to include a list of default CAs to trust, covering most situations, so as long as the website operator purchased a certificate from one of these CAs.

The quick fix

There are two ways to solve this problem. Firstly, we can simply configure cURL to accept any server(peer) certificate. This isn’t optimal from a security point of view, but if you’re not passing sensitive information back and forth, this is probably alright. Simply add the following line before calling curl_exec():

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

This basically causes cURL to blindly accept any server certificate, without doing any verification as to which CA signed it, and whether or not that CA is trusted. If you’re at all concerned about the data you’re passing to or receiving from the server, you’ll want to enable this peer verification properly. Doing so is a bit more complicated.

The proper fix

The proper fix involves setting the CURLOPT_CAINFO parameter. This is used to point towards a CA certificate that cURL should trust. Thus, any server/peer certificates issued by this CA will also be trusted. In order to do this, we first need to get the CA certificate. In this example, I’ll be using the https://api.del.icio.us/ server as a reference.

First, you’ll need to visit the URL with your web browser in order to grab the CA certificate. Then, (in Firefox) open up the security details for the site by double-clicking on the padlock icon in the lower right corner:

Then click on “View Certificate”:

Bring up the “Details” tab of the cerficates page, and select the certificate at the top of the hierarchy. This is the CA certificate.

Then click “Export”, and save the CA certificate to your selected location, making sure to select the X.509 Certificate (PEM) as the save type/format.

Now we need to modify the cURL setup to use this CA certificate, with CURLOPT_CAINFO set to point to where we saved the CA certificate file to.

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/CAcerts/BuiltinObjectToken-EquifaxSecureCA.crt");

The other option I’ve included, CURLOPT_SSL_VERIFYHOST can be set to the following integer values:

If you have CURLOPT_SSL_VERIFYPEER set to false, then from a security perspective, it doesn’t really matter what you’ve set CURLOPT_SSL_VERIFYHOST to, since without peer certificate verification, the server could use any certificate, including a self-signed one that was guaranteed to have a CN that matched the server’s host name. So this setting is really only relevant if you’ve enabled certificate verification.

This ensures that not just any server certificate will be trusted by your cURL session. For example, if an attacker were to somehow redirect traffic from api.delicious.com to their own server, the cURL session here would not properly initialize, since the attacker would not have access to a server certificate (i.e. would not have the private key) trusted by the CA we added. These steps effectively export the trusted CA from the web browser to the cURL configuration.

More information

If you have the CA certificate, but it is not in the PEM format (i.e. it is in a binary or DER format that isn’t Base64-encoded), you’ll need to use something like OpenSSL to convert it to the PEM format. The exact command differs depending on whether you’re converting from PKCS12 or DER format.

There is a CURLOPT_CAPATH option that allows you to specify a directory that holds multiple CA certificates to trust. But it’s not as simple as dumping every single CA certificate in this directory. Instead, they CA certificates must be named properly, and the OpenSSL c_rehash utility can be used to properly setup this directory for use by cURL.

http 使用curl发起https请求 error 60 51相关推荐

  1. http 使用curl发起https请求

    今天一个同事反映,使用curl发起https请求的时候报错:"SSL certificate problem, verify that the CA cert is OK. Details: ...

  2. 用curl发起https请求

    使用curl发起https请求 使用curl如果想发起的https请求正常的话有2种做法: 方法一.设定为不验证证书和host. 在执行curl_exec()之前.设置option $ch = cur ...

  3. WAMP安装curl扩展并发起https请求

    wamp安装curl扩展的方法: http://blog.csdn.net/superuser007/article/details/5781095 安装出现 PHP Extension " ...

  4. java用HttpURLConnection发起HTTPS请求并跳过SSL证书,解决:unable to find valid certification path to requested targ

    java用HttpURLConnection发起HTTPS请求并跳过SSL证书 问题出现:unable to find valid certification path to requested ta ...

  5. Shell脚本curl发起http请求并保存到文件/追加到已有文件

    1.Shell脚本curl发起http请求,保存到文件 #从nacos配置中心拉取配置数据 #!/bin/bash RESULT=$(curl -s "http://localhost:88 ...

  6. linux抓post命令,Linux 使用curl发起post请求的4个常用方式

    引言 cURL是一种命令行实用程序,用于使用一种受支持的协议,从远程服务器传输数据,或将数据传输到远程服务器.默认情况下,已安装在macOS和大多数Linux发行版上. 开发人员可以使用cURL来测试 ...

  7. php curl模拟https请求

    https请求(支持GET和POST) function http_request($url,$data = null){$curl = curl_init();curl_setopt($curl, ...

  8. 使用proxy转发post请求_3分钟短文 | Linux 使用curl发起post请求的4个常用方式

    引言 cURL是一种命令行实用程序,用于使用一种受支持的协议,从远程服务器传输数据,或将数据传输到远程服务器.默认情况下,已安装在macOS和大多数Linux发行版上. 开发人员可以使用cURL来测试 ...

  9. php curl 发送https请求失败,php的curl扩展无法发起https请求

    很奇怪的是,file_get_content函数可以对https地址发起请求并且收到响应报文,但是curl就不可以,这是什么原因呢?我已经安装了openssl扩展. function fetch($u ...

最新文章

  1. 1.AutoRec: Autoencoders Meet Collaborative Filtering论文解读以及AutoRec代码实现(pytorch)
  2. Spring编程式和声明式事务实例讲解
  3. iOS:极光推送控制器跳转
  4. windows 7下同时安装visual studio 2012和2010
  5. scapy on openwrt
  6. ubuntu 查看网卡 数据包处理 速度
  7. SAP生产订单预留(下)
  8. sklearn综合示例9:分类问题的onehot与预测阈值调整
  9. 面向全球用户的Teams app之Culture数字篇
  10. PDF编辑器哪个好,如何在PDF中插入图片背景
  11. 视觉SLAM笔记(38) 3D-3D: ICP
  12. 怎么做软件安全性测试
  13. AcWing1073.树的中心(树形DP)题解
  14. SQL按字段分组取最大(小)值记录(重复记录)
  15. C++中for循环的5种语法
  16. Hibernate(转载)
  17. 计算机桌面常见故障,电脑常见故障问题以及解决办法
  18. 项目实战第一讲:如何优雅地记录操作日志
  19. DNS的作用是什么?为什么一定要配置DNS才能上网
  20. Justinmind使用教程(1)——概述部分

热门文章

  1. 站长必知的八大社会心理学效应
  2. 安装McAfee 8.7i 提示错误1920怎么办?
  3. android对象关系映射框架ormlite之一对多(OneToMany)
  4. 批评一下 dearbook
  5. java高质量图片压缩
  6. Failed to execute goal org.apache.maven.plugins:ma
  7. 精品软件推荐 Desktop Central - Free Windows Admin Tools
  8. jsp当参数为空的时候默认显示值
  9. 图解:SQL SERVER2005的安装
  10. 如何避免开源安全噩梦?