我正在尝试从贷款俱乐部下载贷款统计文件。他们提供了文件的两个版本,一个是安全版本,另一个是常规版本。为了下载安全版本,我必须登录。
url的内容如下:https://resources.lendingclub.com/secure/LoanStats3a_securev1.csv.zip?signature=foo&发布=条形
每次登录和浏览页面时,"签名"one_answers"已发布"似乎都会发生变化。
我的解决方案是登录到该网站,转到下载页面并获得带有查询字符串的完整url。
我尝试了以下代码登录:
$username = "myemail@example.com";
$password = "mypassword";
$url='https://www.lendingclub.com/account/login.action?login_email='.$username.'&login_password='.$password;
$agent= 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
$httpcode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
var_dump($httpcode);
var_dump($result);
return;
我得到了200的http响应。和
int(200) string(32454) "
This website does not support this version of Internet Explorer. Please upgrade to the latest version for a better experience. Upgrade Now
...
当我在浏览器中访问该网站时,我通过嗅探标头获得了用户代理字符串。
我花了一下午的大部分时间在谷歌上搜索,试图找到解决这个问题的方法。我试着用饼干,但没用。
还有什么我可以尝试的想法吗?
谢谢。
在尝试了更多的事情之后,我终于找到了解决问题的方法。
我把它张贴在这里,以备将来有人遇到类似的问题时参考:
我试图在第一个cURL请求中使用我的登录凭据(用户名和密码)登录Lending Club。该网站不允许我登录,一直将我重定向到登录屏幕。
我的解决方案是使用2个cURL请求。第一个没有我凭据的登录页面。我这样做是为了保存网站设置的cookie。然后我在我的第二个cURL请求中使用了这个cookie——这个请求带有我的登录凭据。它奏效了。
请参阅下面的工作代码:
$cookie = 'cookie.txt';
$url = 'https://www.lendingclub.com/account/login.action';
//first cURL request - no login credentials. Used only to get the cookie from site.
$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 - with login credentials. Added cookie obtained from first cURL request above.
$fields = array(
'login_email' => 'email@example.com',
'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);
此外,我仍然收到消息,该网站不支持此版本的Internet Explorer。但这似乎只是一个警告。我仍然可以登录。
谢谢。