首页 > 解决方案 > CURL 登录到使用令牌的页面

问题描述

我正在使用 php curl 尝试登录到使用令牌的表单。我尝试了不同的组合,其中一些来自 StackOverflow,但似乎无法做到正确。

到目前为止,这是我的代码:

$html = new simple_html_dom();

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://www.example.de/My-account');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, '');

$response = curl_exec($ch);
$html->load($response);

foreach($html->find('input[name="jtl_token"]') as $content){
    $token = $content->value;
}

$credentials = [
    'jtl_token' => $token,
    'email' => 'my@email.com',
    'passwort' => 'password',
    'login' => 1
];

curl_setopt($ch, CURLOPT_URL, 'https://www.example.de/My-account');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($credentials));
curl_setopt($ch, CURLOPT_COOKIEFILE, '');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

echo $response;

我正在使用simple_html_dom类来获取令牌,然后我只是尝试将其发送到该页面但它不起作用,响应是登录前的页面。

谢谢你。

标签: phpphp-curl

解决方案


推荐阅读