1. 程式人生 > >http 使用curl發起https請求 error 60 51

http 使用curl發起https請求 error 60 51

本地ssl判別證書太舊,導致連結報錯ssl證書不正確。

我們需要下載新的ssl 本地判別檔案

放到 程式檔案目錄

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)

為了防止某天該文章被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 even Gopher. (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 as XML-RPC or REST to query a resource. For example, 

Delicious offers 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:

  • 0: Don’t check the common name (CN) attribute
  • 1: Check that the common name attribute at least exists
  • 2: Check that the common name exists and that it matches the host name of the server

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

本地ssl判別證書太舊,導致連結報錯ssl證書不正確。 我們需要下載新的ssl 本地判別檔案 放到 程式檔案目錄 curl 增加下面的配置    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,true); ;    curl_setopt($ch,CURLOPT_CAINF

發送http請求和https請求的工具類

stack urlencode array dpa pre public char set new t package com.haiyisoft.cAssistant.utils; import java.io.IOException;import java.util.A

NodeMCU網路請求http請求和Https請求的示例程式碼(C++開發)

沒什麼可說的就是開發中需要做https請求,但是幾乎沒有找到例子。最後就研究開原始碼標頭檔案、等文件。實現了https請求。看到過一個部落格用的#include <WiFiClientSecure.h>但是那個封裝有問題。請求返回特別慢。後來看到NodeMCU有自

php curl模擬https請求

$curl = curl_init(); //設定抓取的url curl_setopt($curl, CURLOPT_URL, 'http:........'); //設定標頭檔案的資訊作為資料流輸出 curl_setopt($curl, CURLOPT_HEADER, 0)

php cURL模擬https請求報錯

最近在做微信開發,通過cURL請求建立微信自定義選單的時候結果報錯,後來查資料總要解決。 環境:阿里 centos6.4 php5.3 nginx 報錯一: Problem with the SSL CA cert (path? access… 解決方式: 1、不管之前安裝

android開發,http請求和https請求有什麼區別

① http效率更高,https安全性更高。 ② http是超文字傳輸協議,https是安全超文字傳輸協議, ③ http資訊是以明文方式傳遞,https是使用ssl加密傳輸協議傳輸資料,也就是通過第三方工具可以擷取篡改http傳輸的資料,而https即使截獲了沒有金鑰也白

使用httpclient發起https請求時peer not authenticated,handshake_failure

轉載地址:http://www.cnblogs.com/metoy/p/6238061.html 一、前述   使用httpclient發起https請求時,可能會遇到如下異常: javax.net.ssl.SSLPeerUnverifiedException: pee

curl實現httphttps請求的方法

每次要使用curl的時候,總要查一堆資料。 現在將常用的幾句儲存下來,省的每次都去谷歌。 常規curl請求: $url = '//www.jb51.net'; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_s

如何在java中發起httphttps請求

一般呼叫外部介面會需要用到http和https請求。 一.發起http請求 1.寫http請求方法 //處理http請求 requestUrl為請求地址 requestMethod請求方式,值為"GET"或"POST" public static String h

如何在java中發起httphttps請求 配置信任

記錄下專案中遇到的問題一般呼叫外部介面會需要用到http和https請求。一.發起http請求1.寫http請求方法//處理http請求  requestUrl為請求地址  requestMethod請求方式,值為"GET"或"POST"    publicstatic St

Qt發起Http/Https請求

我們 cati 查看 sch 變量 app font manage 下載 1. BurpSuite抓包1.1 設置代理burpsuite代理設置瀏覽器代理設置(chrome),其他瀏覽器同理。地址欄輸入 chrome://settings/,打開代理設置設置代理端口如果使用

curl訪問HTTPS站點並登錄(對HTTP返回的結果特別清楚)

type .com nss int lai on() tftp cap 參數 開發網站,少不了測試。現在的網站為了加強安全性,都啟用了HTTPS協議。所謂HTTPS,也就是HTTP文本在SSL協議中傳輸。用curl命令行來測試HTTPS站點是個很有用的功能,寫點腳本,就可以

Jmeter Web 性能測試入門 (二):Fiddler 抓取 http/https 請求

功能 wrap 3-9 req safari itl box 移動 移動設備 jmeter自帶了攔截request的功能,並且也有對應的tool:badboy 可以用。但由於我經常做移動端的項目,個人還是習慣用fiddler來收集request。 官網下載並安裝Fiddle

HttpClient 發送 HTTPHTTPS 請求的簡單封裝

文檔 fault con load obj gpo n) content ble 序 近期這幾周。一直在忙同一個項目。剛開始是了解需求。需求有一定了解之後,就開始調第三方的接口。因為第三方給提供的文檔非常模糊,在調接口的

使用谷歌瀏覽器開發者工具分析 http/https 請求耗時

web1、打開工具2、各部分的含義 參考文章:https://developers.google.com/web/tools/chrome-devtools/network-performance/reference#timing-explanation使用谷歌瀏覽器開發者工具分析 http/https 請求

封裝HttpClient進行http請求https請求

src empty one key-value fin finally 發送post請求 工具類 catch 一.https忽略證書 /** * 用於進行Https請求的HttpClient * * @author joey * */ public class

nginx學習筆記(一) 用nginx實現本地https請求http請求

error erro 需要 ror har file key media nginx代理 接到項目需求需要將一些https請求利用nginx代理到http接口上,因此要在本地上搭環境進行測試,現在將該過程記錄一下。 生成證書 1. 使用openssl生成密鑰privkey.

easywechat 微信開發上傳素材時 PHP出現 cURL error 60 的解決辦法

Fatal error: Uncaught GuzzleHttp\Exception\RequestException: cURL error 60: SSL certificate problem: unable to get local issuer certificate cURL erro

Tomcat 配置http請求自動轉為https請求並解決80端口占用問題

第一步.配置Tomcat的https請求,我的這篇部落格記錄了一下相關操作:https://blog.csdn.net/LJX_ahut/article/details/82153895   第二步 修改預設埠 由於http協議的預設埠是80(Tomcat預設配置

curl 區分https/http

https: function vPost($url,$data){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); // 要訪問的地址 curl_setopt($curl, CURLOPT_SSL_VER