【发布时间】:2017-03-01 23:42:47
【问题描述】:
我一直在尝试使用 cURL 从该网站获取一些页面数据。页面落后于授权,使用 cookie,没有 SSL。
我检查了很多设置 php cURL 脚本的手册和示例,但似乎都没有工作。
每次我运行我的脚本时,cookie 文件都会更新,但结果却是空字符串。如果我将 CURLOPT_FOLLOWLOCATION 设置为 1,我会得到登录页面。所以我假设,原始脚本返回重定向到登录页面。
我尝试弄乱 CURLOPT_USERAGENT、CURLOPT_REFERER,但没有帮助。
另外,如果我手动设置 CURLOPT_COOKIE、PHPSESSID(来自使用浏览器和人工输入的真实登录会话),它工作正常。
所以,这是我的代码:
<?php
set_time_limit(10);
define('USERNAME', 'username');
define('PASSWORD', 'password');
define('USER_AGENT', 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.2309.372 Safari/537.36');
define('COOKIE_FILE', 'cookie.txt');
define('LOGIN_FORM_URL', 'http://website/auth');
define('LOGIN_ACTION_URL', 'http://website/distribution/index');
$postValues = array(
'login_msisdn' => USERNAME,
'password' => PASSWORD
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, LOGIN_FORM_URL);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postValues));
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_COOKIEJAR, realpath(COOKIE_FILE));
//curl_setopt($curl, CURLOPT_COOKIE, "PHPSESSID=relkdrgg94gfdgfg834g");
curl_setopt($curl, CURLOPT_USERAGENT, USER_AGENT);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_REFERER, LOGIN_FORM_URL);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0);
curl_exec($curl);
if(curl_errno($curl)){
throw new Exception(curl_error($curl));
}
curl_close($curl);
$curl = curl_init()
curl_setopt($curl, CURLOPT_URL, LOGIN_ACTION_URL);
curl_setopt($curl, CURLOPT_COOKIEFILE, realpath(COOKIE_FILE));
curl_setopt($curl, CURLOPT_USERAGENT, USER_AGENT);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($curl);
echo $html;
【问题讨论】: