2016-04-02 7 views
1

Ich versuche, eine Zip-Datei von Lending Club-Website (www.lendingclub.com) herunterladen.PHP cURL Herunterladen von 0 Bytes von https-Seite zugänglich nur mit Benutzername und Passwort

Bisher habe ich festgestellt, dass ich eingeloggt sein muss, um die Datei herunterladen zu können. Die Download-URL ist so etwas wie:

https://resources.lendingclub.com/secure/LoanStats3a_securev1.csv.zip?signature=Cw8BbuYYmWJW7EOZzeSBb3WUi1k%3D&issued=1459640781435

Unterschrift und ausgegeben Felder jedes Mal, wenn ich auf die Site anmelden ändern. Wenn ich die URL in ein anderes Browserfenster kopiere und einfüge, kann ich die Datei herunterladen.

Ich glaube, die Website überprüft eine gültige Signatur und ausgestellt, bevor es mir ermöglicht, die Datei herunterzuladen.

Ich kann mich auf der Website anmelden und zu der Seite navigieren, auf der sich die Datei befindet. Ich benutze cURL dazu. Ich kann die spezifische URL mit der Signatur und den ausgegebenen Feldern erfassen. Jedoch, wenn ich die cURL, um es herunterzuladen, bekomme ich eine Antwort mit einem http-Code 401.

Es scheint, dass die Website nicht erkennt, dass ich eingeloggt bin und antwortet mit dem 401-Code.

Unten ist der Code Ich verwende anmelden und die Datei zum Download:

$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; 

Meine Antwort ist so etwas wie:

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) 

Irgendwelche Vorschläge auf, was ich anders machen könnte das zum Download Datei?

Danke.

UPDATE # 1 - Implementierungsrat von draw010 aus dem Kommentarbereich.

Ich navigierte auf die Download-Seite im Browser und klickte auf den Link, um die Datei herunterzuladen. Unten ist der Header mein Browser gesendet:

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> 

ich dann die letzte cURL Anfrage geändert:

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; 

noch das gleiche Problem. Es gibt Code 401 zurück.

+0

Versuchen Sie, weitere HTTP-Header (einschließlich User-Agent) hinzuzufügen, um es wie einen echten Browser aussehen zu lassen. Außerdem müssen Sie nicht jedes Mal ein neues cURL-Handle erstellen. Sie können dasselbe für jede Anfrage erneut verwenden, um die Arbeit zu vereinfachen. – drew010

+0

@ drew010 danke für die Antwort. Ich habe den gesendeten Header überprüft, als ich ihn aus dem Browserfenster heruntergeladen und der cURL-Anfrage hinzugefügt habe. Funktioniert immer noch nicht. Ich habe die Frage mit den Ergebnissen aktualisiert. – jdias

+0

Können Sie bestätigen, dass die Cookie-Datei erstellt wird? Ich kann morgen versuchen, einen Account zu erstellen und zu sehen, ob ich es zum Laufen bringen kann. – drew010

Antwort

1

Ich habe das Problem gefunden. Es hatte nichts mit der cURL-Anfrage oder dem 401-Code zu tun.

ich die URL für die Datei bekomme ich durch Analysieren der Ausgabe eines cURL Anfrage (siehe unten) herunterladen wollte:

$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]; 

Das Problem ist, dass die URL hatte eine „&“, die codiert wurde als "& Ampere". Als ich die Saite auf dem Bildschirm wiedergab, konnte ich nur den "&", nicht den "& Amp" sehen.

So mit strlen und strpos nach dem Spielen fand ich das Problem und löste es durch die Linie ersetzt:

$url3a = $parts[0][0]; 

mit

$url3a = htmlspecialchars_decode($parts[0][0]); 

dies das Problem gelöst.

Danke.

Verwandte Themen