通过cURL登录后如何导航到其他页面? [英] How do I navigate to different pages after logging in via cURL?

查看:78
本文介绍了通过cURL登录后如何导航到其他页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

登录页面- https://b2b.chiemsee.com/customer/account/login /
成功登录页面后- https://b2b.chiemsee .com / customer / account / loginPost /
我要转到的页面- https: //b2b.chiemsee.com/damen

我已经提出了3个单独的请求:
1)获取CSRF令牌。
2)进行登录。
3)要导航到我要访问的网址。

I have made 3 separate requests: 1) To get the CSRF token. 2) To make the login. 3) To navigate to the url I want to go to.

直到我添加了第三个请求,登录名正常,并使我进入 loginPost / 页面,但现在它带我回到 login / 页面。

Until I added the third request, the login was working fine and taking me to loginPost/ page but now it takes me back to the login/ page. Does it have something to do with the cookie in the third request?

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;


推荐答案


2)使

2) To make the login.

该代码从一开始就不起作用,您只是没有足够的登录错误检查代码来实现它。

that code never worked in the first place, you just have insufficient login-error-checking code to realize it.

替换

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

,您会看到登录时出错。

and you'll see that you get an error during login.

这篇关于通过cURL登录后如何导航到其他页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆