我正在写一个cURL脚本来访问Fannie Mae网站上的当前天利率,即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时,没有标题。
尝试通过在这里搜索找到的7-8个解决方案,同时阅读cURL上的php文档并尝试在那里列出几个解决方法,没有运气。
我需要能够使用CURLOPT_SSL_VERIFYPEER检索标题并最终检索正文
感谢任何帮助。
<?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);
}
?>
这是CURLOPT_SSL_VERIFYPEER设置为false时收到的标头,如果为true则为空白
收到标题:HTTP / 1.1 200 OK 收到标题:日期:星期四,2013年9月19日00:40:16 GMT 收到标题:服务器:Apache 收到标题:Set-Cookie:JSESSIONID = 4297C1E1760A836F691FE821FBF8B805.cportal-cl01;路径= /;安全;仅Http 收到的标题:Cache-Control:no-store 收到标题:到期日:1969年12月31日星期三23:59:59 GMT 收到标题:Pragma:no-cache 收到标题:X-FRAME-OPTIONS:SAMEORIGIN 收到标题:内容 - 语言:en-US 收到标题:内容长度:9344 收到的标题:Content-Type:text / html; charset = ISO-8859-1 收到标题:
答案 0 :(得分:11)
您使用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);
}