登录页面-https://b2b.chiemsee.com/customer/account/login/ 成功登录页面后-https://b2b.chiemsee.com/customer/account/loginPost/ 我要转到的页面-https://b2b.chiemsee.com/damen
我提出了3个单独的请求: 1)获取CSRF令牌。 2)进行登录。 3)导航到我要访问的网址。
在我添加第三个请求之前,登录正常,将我带到loginPost/
页,但现在它带我回到login/
页。它与第三个请求中的cookie有关吗?
PHP:-
// login request one to get form_key
include('simple_html_dom.php');
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, "https://b2b.chiemsee.com/customer/account/login/");
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, true); // to allow redirections
curl_setopt($ch2, CURLOPT_COOKIEJAR, 'cookie.txt'); // to save cookie data for login
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true); // to get the html
$response2 = curl_exec($ch2);
curl_close($ch2);
$html = new simple_html_dom();
$html->load($response2);
$val = $html->find('input[name=form_key]');
$form_key = $val[0]->value;
// login request two to send form_key, username and password
$data = array(
'form_key' => $form_key,
'login[username]' => 'user',
'login[password]' => 'pass',
'send' => 'Anmelden'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://b2b.chiemsee.com/customer/account/loginPost/");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // to allow redirections
curl_setopt($ch, CURLOPT_POST, true); // to send info
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); // to read data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // to get the html
$response = curl_exec($ch);
if (curl_error($ch)) {
$error_msg = curl_error($ch);
var_dump($error_msg);
exit;
}
curl_close($ch);
// request three to open first category
$ch3 = curl_init();
curl_setopt($ch3, CURLOPT_URL, "https://b2b.chiemsee.com/damen");
curl_setopt($ch3, CURLOPT_FOLLOWLOCATION, true); // to allow redirections
curl_setopt($ch3, CURLOPT_COOKIEFILE, 'cookie.txt'); // to read data
curl_setopt($ch3, CURLOPT_RETURNTRANSFER, true); // to get the html
$response3 = curl_exec($ch3);
if (curl_error($ch3)) {
$error_msg3 = curl_error($ch3);
var_dump($error_msg3);
exit;
}
curl_close($ch3);
echo $response3;
答案 0 :(得分:0)
我发现了问题所在。 我没有使用以下命令将第二个请求中发送的新数据(用户名和密码)保存在cookie文件中:
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); // to save cookie data for login
更新的代码:
// login request one to get form_key
include('simple_html_dom.php');
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, "https://b2b.chiemsee.com/customer/account/login/");
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, true); // to allow redirections
curl_setopt($ch2, CURLOPT_COOKIEJAR, 'cookie.txt'); // to save cookie data for login
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true); // to get the html
$response2 = curl_exec($ch2);
curl_close($ch2);
$html = new simple_html_dom();
$html->load($response2);
$val = $html->find('input[name=form_key]');
$form_key = $val[0]->value;
// login request two to send form_key, username and password
$data = array(
'form_key' => $form_key,
'login[username]' => 'user',
'login[password]' => 'pass',
'send' => 'Anmelden'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://b2b.chiemsee.com/customer/account/loginPost/");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // to allow redirections
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // to avoid error
curl_setopt($ch, CURLOPT_POST, true); // to send info
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); // to read data
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); // to save cookie data for login
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // to get the html
$response = curl_exec($ch);
if (curl_error($ch)) {
$error_msg = curl_error($ch);
var_dump($error_msg);
exit;
}
curl_close($ch);
//echo $response;
// request three to open first category
$ch3 = curl_init();
curl_setopt($ch3, CURLOPT_URL, "https://b2b.chiemsee.com/damen");
curl_setopt($ch3, CURLOPT_FOLLOWLOCATION, true); // to allow redirections
curl_setopt($ch3, CURLOPT_COOKIEFILE, 'cookie.txt'); // to read data
curl_setopt($ch3, CURLOPT_RETURNTRANSFER, true); // to get the html
$response3 = curl_exec($ch3);
if (curl_error($ch3)) {
$error_msg3 = curl_error($ch3);
var_dump($error_msg3);
exit;
}
curl_close($ch3);
echo $response3;
答案 1 :(得分:0)
2)进行登录。
该代码从来没有起作用,您只是没有足够的登录错误检查代码来实现它。
替换
$response = curl_exec($ch);
if (curl_error($ch)) {
$error_msg = curl_error($ch);
var_dump($error_msg);
exit;
}
curl_close($ch);
使用
$response = curl_exec($ch);
if (curl_error($ch)) {
$error_msg = curl_error($ch);
var_dump($error_msg);
exit;
}
$domd=@DOMDocument::loadHTML($response);
$xp=new DOMXPath($domd);
$login_errors=array();
foreach($xp->query("//*[contains(@class,'error-msg')]") as $login_error){
$login_error=trim($login_error->textContent);
if(!empty($login_error)){
$login_errors[]=$login_error;
}
}
if(!empty($login_errors)){
throw new \RuntimeException("login errors: ".print_r($login_errors,true));
}
curl_close($ch);
,您会看到登录时出错。