私はLending Clubのウェブサイト(www.lendingclub.com)からzipファイルをダウンロードしようとしています。PHP cURLをダウンロードするhttpsページから0バイトしかアクセスできないユーザ名とパスワードでのみアクセス可能
これまでのところ、私はファイルをダウンロードするためにログインする必要があることを確立しました。ダウンロードURLのようなものです:
署名と発行フィールドは、私がサイトにログインするたびに変更します。 URLをコピーして別のブラウザウィンドウに貼り付けると、ファイルをダウンロードできます。
私はサイトが有効な署名をチェックし、ファイルをダウンロードする前に発行されたと考えます。
私はサイトにログインし、ファイルがあるページに移動できます。私はそうするためにcURLを使用しています。私は署名と発行されたフィールドで特定のURLをキャプチャすることができます。しかし、私がcURLをダウンロードすると、httpコード401の応答が返ってきます。
私はログインしていると認識していないと思われ、401コードで応答します。以下は
私がログインすると、ファイルをダウンロードするために使用していたコードです:私はダウンロードして異なるが何ができるか上の任意の提案
array(23) { ["url"]=> string(135) "https://resources.lendingclub.com/secure/LoanStats3a_securev1.csv.zip?signature=LoEEC1JOFCjfwhv3y6atOMnD2rA%3D&issued=1459641477069" ["content_type"]=> NULL ["http_code"]=> int(401) ["header_size"]=> int(201) ["request_size"]=> int(192) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(0.229254) ["namelookup_time"]=> float(0.026935) ["connect_time"]=> float(0.065868) ["pretransfer_time"]=> float(0.187812) ["size_upload"]=> float(0) ["size_download"]=> float(0) ["speed_download"]=> float(0) ["speed_upload"]=> float(0) ["download_content_length"]=> float(0) ["upload_content_length"]=> float(0) ["starttransfer_time"]=> float(0.22921) ["redirect_time"]=> float(0) ["certinfo"]=> array(0) { } ["primary_ip"]=> string(14) "216.115.73.151" ["redirect_url"]=> string(0) "" } bool(true)
:
$cookie = 'cookie.txt';
$url = 'https://www.lendingclub.com/account/login.action';
//first cURL request to obtain cookie
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); // Stores cookies in the temp file
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
//second cURL request to submit my login credentials and login to the site
$fields = array(
'login_email' => '[email protected]',
'login_password' => 'mypassword',
);
$fields_string = '';
foreach($fields as $key=>$value)
{
$fields_string .= $key . '=' . $value . '&';
}
rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); //Uses cookies from the temp file
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); // Stores cookies in the temp file
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Tells cURL to follow redirects
$output = curl_exec($ch);
//third cURL request to get url where the file I want to download is.
$url = 'https://www.lendingclub.com/info/download-data.action';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); //Uses cookies from the temp file
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); // Stores cookies in the temp file
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
//regular expression to capture the url (with signature and issued fields)
$regex = '/\b(https?|ftp|file):\/\/resources\.lendingclub\.com\/secure[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i';
preg_match_all($regex, $output, $parts);
$url3a = $parts[0][0];
OutputMsg($url3a); //output the url to confirm I captured the whole url including the query string
//fourth cURL to download the zip file
set_time_limit(0); //prevent timeout
$fp = fopen (dirname(__FILE__) . '/' . 'testfile.zip', 'w+');
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 5040);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL, $url3a);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); //Uses cookies from the temp file
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); // Stores cookies in the temp file
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Tells cURL to follow redirects
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
fclose($fp);
var_dump($info);
var_dump($output);
return;
私の応答のようなものですファイル?
ありがとうございました。
UPDATE#1 - コメントセクションからdrew010のアドバイスを実装します。
ブラウザのダウンロードページに移動し、リンクをクリックしてファイルをダウンロードしました。以下は私のブラウザが送信され、ヘッダである:
set_time_limit(0); //prevent timeout
$fp = fopen (dirname(__FILE__) . '/' . 'testfile.zip', 'w+');
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 5040);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL, $url3a);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); //Uses cookies from the temp file
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); // Stores cookies in the temp file
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Tells cURL to follow redirects
$headers = array(
"Host: resources.lendingclub.com",
"Connection: keep-alive",
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Upgrade-Insecure-Requests: 1",
"User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36",
"Referer: https://www.lendingclub.com/info/download-data.action",
"Accept-Encoding: gzip, deflate, sdch",
"Accept-Language: en-US,en;q=0.8"
);
curl_setopt($ch, CURLOPT_HEADER, $headers);
$output = curl_exec($ch);
if(curl_errno($ch)){
echo 'Curl error: ' . curl_error($ch);
} else {
echo 'no Curl error';
}
$info = curl_getinfo($ch);
curl_close($ch);
fclose($fp);
var_dump($info);
var_dump($output);
return;
それでも同じ問題:
GET /secure/LoanStats3a_securev1.csv.zip?signature=4TWzCzq1bGdLXb3l76L6T6ElX1c%3D&issued=1459660640149 HTTP/1.1
Host: resources.lendingclub.com
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36
Referer: https://www.lendingclub.com/info/download-data.action
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Cookie: <deleted for privacy>
私は最後のcURLリクエストを変更しました。コード401が返されます。
HTTPヘッダー(User-Agentを含む)をさらに追加して、実際のブラウザのように見せてください。また、毎回新しいcURLハンドルを作成する必要はなく、リクエストごとに同じcURLハンドルを再利用すると、作業が簡単になります。応答のために – drew010
@ drew010ありがとう。私は、ブラウザウィンドウからダウンロードしてcURL要求に追加したときに送られたヘッダーを調べました。まだ動作しません。質問を結果に更新しました。 – jdias
クッキーファイルが作成されていることを確認できますか?私は明日アカウントを作って、それを動作させることができるかどうか見てみることができます。 – drew010