8

https であるファニーメイの Web サイトから現在の金利にアクセスするための cURL スクリプトを作成しています。CURLOPT_SSL_VERIFYPEER を通過できませんでした。true); オプション。

ユーザー名やパスワードは必要ありませんが、SSL 検証をオンにする必要があります。

XAMPP 開発サーバーでのテスト。

FFを使用してWebサイトから.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; パス=/; 安全; HttpOnly 受信ヘッダー: Cache-Control: no-store 受信ヘッダー: Expires: Wed, 31 Dec 1969 23:59:59 GMT 受信ヘッダー: プラグマ: no-cache 受信ヘッダー: X-FRAME-OPTIONS: SAMEORIGIN 受信ヘッダー: Content-言語: en-US 受信ヘッダー: Content-Length: 9344 受信ヘッダー: Content-Type: text/html;charset=ISO-8859-1 受信ヘッダー:

4

1 に答える 1

12

を使用して本体を除外しています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);
}
于 2013-09-19T03:02:01.843 に答える