在 cURL PHP 中为 https 启用 SSL 连接(标题空白)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18884821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:24:28  来源:igfitidea点击:

Enable SSL connection for https in cURL PHP (header blank)

phpcurlsslhttps

提问by Optionwiz

I am writing a cURL script to access the current days interest rate from the Fannie Mae website which is https. I havent been able to get past the CURLOPT_SSL_VERIFYPEER, true); option.

我正在编写一个 cURL 脚本,以从 https 的 Fannie Mae 网站访问当前的利率。我没能通过 CURLOPT_SSL_VERIFYPEER,真的);选项。

No username or password is required, however I need SSL verification turned on.

不需要用户名或密码,但我需要开启 SSL 验证。

Testing on XAMPP dev server.

在 XAMPP 开发服务器上进行测试。

I have downloaded the .crt and .pem certs from the website using FF and saved them in the same source dir and pointed to both using CURLOPT_CAINFO, no luck

我已经使用 FF 从网站下载了 .crt 和 .pem 证书并将它们保存在同一个源目录中,并使用 CURLOPT_CAINFO 指向两者,不走运

I downloaded the latest cacert.pem file from http://curl.haxx.se/ca/cacert.pemand pointed to that as well using CURLOPT_CAINFO, no luck.

我从http://curl.haxx.se/ca/cacert.pem下载了最新的 cacert.pem 文件,并使用 CURLOPT_CAINFO 指向该文件,不幸的是。

If I turn CURLOPT_SSL_VERIFYPEER, to false I can retrieve the header (see below), however when I set it to true there is no header.

如果我将 CURLOPT_SSL_VERIFYPEER 设置为 false,我可以检索标头(见下文),但是当我将其设置为 true 时,则没有标头。

Tried about 7-8 solutions found by searching on here along with reading the php documention on cURL and trying several workarounds listed there, no luck.

通过在此处搜索以及阅读 cURL 上的 php 文档并尝试此处列出的几种解决方法,尝试了大约 7-8 个解决方案,但没有成功。

I need to be able to retrieve the header and eventually the body using CURLOPT_SSL_VERIFYPEER, true

我需要能够使用 CURLOPT_SSL_VERIFYPEER, true 来检索标题和最终的正文

Any help is appreciated.

任何帮助表示赞赏。

<?php

// script is designed to access an https site and retrieve the last table showing the most recent 90 day commitment for the Fannie Mae 30 year fixed rate mortgage.  Site is designed to work with cookies and has a valid SSL cert.

//turn error reporting on
error_reporting(E_ALL); ini_set("display_errors", 1); 

// cookie file name/location
$cookie_file_path = "cookies.txt";

// verify if cookie file is accessible and writable
if (! file_exists($cookie_file_path) || ! is_writable($cookie_file_path))
{
    echo 'Cookie file missing or not writable.';
    exit;
}

// url connection
$url = "https://www.fanniemae.com/content/datagrid/hist_net_yields/cur30.html";

// Initiate connection
$ch = curl_init();

// Set cURL and other options
curl_setopt($ch, CURLOPT_URL, $url); // set url
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); // set browser/user agent
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // automatically follow Location: headers (ie redirects)
curl_setopt($ch, CURLOPT_AUTOREFERER, 1); // auto set the referer in the event of a redirect
curl_setopt($ch, CURLOPT_MAXREDIRS, 5); // make sure we dont get stuck in a loop
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); // 10s timeout time for cURL connection
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // allow https verification if true
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // check common name and verify with host name
curl_setopt($ch, CURLOPT_SSLVERSION,3); // verify ssl version 2 or 3
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "VeriSignClass3PublicPrimaryCertificationAuthority-G5.pem"); // allow ssl cert direct comparison
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header'); // get header
curl_setopt($ch, CURLOPT_NOBODY, true); // exclude body
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE); // set new cookie session
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); // file to save cookies in
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); // file to read cookies in

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL connection, save cookie file, free up system resources
curl_close($ch);

// show header
function read_header($ch, $string) {
    print "Received header: $string";
    return strlen($string);
}
?>

This is the header that is received if CURLOPT_SSL_VERIFYPEER is set to false, blank if true

这是在 CURLOPT_SSL_VERIFYPEER 设置为 false 时接收的标头,如果为 true 则为空白

Received header: HTTP/1.1 200 OK Received header: Date: Thu, 19 Sep 2013 00:40:16 GMT Received header: Server: Apache Received header: Set-Cookie: JSESSIONID=4297C1E1760A836F691FE821FBF8B805.cportal-cl01; Path=/; Secure; HttpOnly Received header: Cache-Control: no-store Received header: Expires: Wed, 31 Dec 1969 23:59:59 GMT Received header: Pragma: no-cache Received header: X-FRAME-OPTIONS: SAMEORIGIN Received header: Content-Language: en-US Received header: Content-Length: 9344 Received header: Content-Type: text/html;charset=ISO-8859-1 Received header:

接收到的标头:HTTP/1.1 200 OK 接收到的标头:日期:2013 年 9 月 19 日星期四 00:40:16 GMT 接收到的标头:服务器:Apache 接收到的标头:Set-Cookie:JSESSIONID=4297C1E1760A836F691FE821FBF1B8; 路径=/;安全的; HttpOnly Received header: Cache-Control: no-store Received header: Expires: Wed, 31 Dec 1969 23:59:59 GMT Received header: Pragma: no-cache Received header: X-FRAME-OPTIONS: SAMEORIGIN Received header: Content-语言:en-US 接收头:Content-Length:9344 接收头:Content-Type:text/html;charset=ISO-8859-1 接收头:

回答by subroutines

You're excluding the body by using curl_setopt($ch, CURLOPT_NOBODY, true);. And I don't think you need to install certificate on your machine. The following few lines will give you everything.

您通过使用排除了身体curl_setopt($ch, CURLOPT_NOBODY, true);。而且我认为您不需要在您的机器上安装证书。以下几行将为您提供一切。

$url = 'https://www.fanniemae.com/content/datagrid/hist_net_yields/cur30.html';
$ch = curl_init();    
curl_setopt($ch, CURLOPT_URL, $url); // set url
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); // set browser/user agent    
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header'); // get header
curl_exec($ch);

function read_header($ch, $string) {
    print "Received header: $string";
    return strlen($string);
}