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


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

我正在编写一个cURL脚本,用于从房利美网站(https)访问当前日期的利率。 我没能过CURLOPT_SSL_VERIFYPEER,真的);选择。

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

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

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

我从 http://curl.haxx.se/ca/cacert.pem 下载了最新的cacert.pem文件,并使用CURLOPT_CAINFO指出了这一点,没有运气。

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

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

我需要能够使用 CURLOPT_SSL_VERIFYPEER 来检索标头并最终检索正文,true

任何帮助,不胜感激。

<?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);
}
?>

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

收到的标头:HTTP/1.1 200 OK收到的标头: 日期:2013 年 9 月 19 日星期四 00:40:16 GMT收到的标头:服务器:Apache收到的标头: 设置-cookie: JSESSIONID=4297C1E1760A836F691FE821FBF8B805.cportal-cl01;路径=/;安全;仅 httponly收到的标头:缓存控制:无存储收到的标头: 到期: 星期三, 31 十二月 1969 23:59:59 GMT收到的标头:杂注:无缓存收到的标头:X 帧选项:同源收到的标头:内容语言:en-US收到的标头:内容长度:9344收到的标头:内容类型:text/html;charset=ISO-8859-1收到的标头:

您使用 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);
}