Opt*_*wiz 8 php ssl https curl
我正在写一个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,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);
}
?>
Run Code Online (Sandbox Code Playgroud)
如果CURLOPT_SSL_VERIFYPEER设置为false,则为接收的标头,如果为true,则为空
收到标题:HTTP/1.1 200 OK收到标题:日期:星期四,2013年9月19日00:40:16 GMT收到标题:服务器:Apache收到标题:Set-Cookie:JSESSIONID = 4297C1E1760A836F691FE821FBF8B805.cportal-cl01; 路径= /; 安全; HttpOnly Received header:Cache-Control:no-store Received header:Expires:Wed,19 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收到的标题:
小智 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);
}
Run Code Online (Sandbox Code Playgroud)