CURL POST login prawo-jazdy-360.pl


function exe($content, $start, $end) {
if ($content && $start && $end) {
$r = explode ( $start, $content );
if (isset ( $r [1] )) {
$r = explode ( $end, $r [1] );
return $r [0];
}
return '';
}
}
function executeCurl($url, $postInfo, $type) {
$cookie_file_path = "./cookie.txt";
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_HEADER, false );
curl_setopt ( $ch, CURLOPT_NOBODY, false );
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt ( $ch, CURLOPT_COOKIEJAR, $cookie_file_path );
curl_setopt ( $ch, CURLOPT_COOKIEFILE, $cookie_file_path );
curl_setopt ( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36" );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_REFERER, 'https://www.prawo-jazdy-360.pl/logowanie' );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
curl_setopt ( $ch, CURLOPT_MAXREDIRS, 10 );
if ($type == "post") {
$headers = array (
"Content-Type:application/x-www-form-urlencoded",
"content-length: " . strlen ( $postInfo ) . "" 
); // instead of 0, how could I get the length of the body from curl?
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "POST" );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $postInfo );
}
curl_setopt ( $ch, CURLOPT_NOSIGNAL, 1 );
curl_setopt ( $ch, CURLOPT_TIMEOUT_MS, 40000 );
$page = curl_exec ( $ch );
$curl_errno = curl_errno ( $ch );
/* Check for 404 (file not found). */
$info = curl_getinfo ( $ch );
if ($httpCode == 404 || $curl_errno > 0) {
return 0;
}
return $page;
return 1;
}
$username = "test1@o2.pl";
$password = "haslo123";
$page = executeCurl ( 'https://www.prawo-jazdy-360.pl/logowanie', '', 'get' );
$page = exe ( $page, 'login-left', 'przypomnienie-hasla' );
$token = exe ( $page, '__RequestVerificationToken', '>' );
$token = exe ( $token, 'value="', '"' );
$url = "https://www.prawo-jazdy-360.pl/logowanie";
$postInfo = '{"__RequestVerificationToken": ' . $token . ', "Email":   ' . $username . ', "Password": ' . $password . ', "RememberMe": true }';
$page = executeCurl ( $url, $postInfo, "post" );
$url = "https://www.prawo-jazdy-360.pl/o-nas";
$postInfo = "";
$page = executeCurl ( $url, $postInfo, "get" );

我登录此网站时遇到问题。

不要手动设置Content-Type:application/x-www-form-urlencoded头,让curl为您设置。(请参阅关于CURLOPT_POSTFIELDS的curl_setopt文档)

不要手动设置content-length标头,让curl为您设置。

不要执行curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "POST" );,只需设置CURLOPT_POST。

您设置了Content-Type:application/x-www-form-urlencoded,但$postInfo甚至不接近urlended格式。事实上,它看起来更像JSON编码,但即使是JSON,$username/$password也没有正确地进行JSON编码(它的操作类似于{username:foo}而不是{username:"foo"}),所以我知道这是什么编码,但它绝对不是编码curl告诉服务器它是什么(它的非x-www-form-urlencoded格式)。PS,创建正确的x-www-form-urlencoded字符串的最简单方法是使用http_build_query函数。

登录页面会发送一个名为RememberMe的postfield,但您的代码不会。(似乎是RememberMe=false)

修复这些问题,然后重试。并且不要将其发送为json编码的,将其作为application/x-www-form-urlencoded发送,这就是登录页面所使用的。

使用hhb_curlhttps://github.com/divinity76/hhb_.inc.php/blob/master/hhb_.inc.php,这是一个工作示例代码:

<?php
declare(strict_types = 1);
require_once ('hhb_.inc.php');
$hc = new hhb_curl ();
$hc->_setComfortableOptions ();
$hc->exec ( 'https://www.prawo-jazdy-360.pl/logowanie' );
$html = $hc->getResponseBody ();
$domd = @DOMDocument::loadHTML ( $html );
$inputs = array ();
// get __RequestVerificationToken and RememberMe and whatever else
foreach ( $domd->getElementById ( "login-form" )->getElementsByTagName ( "input" ) as $input ) {
$inputs [$input->getAttribute ( "name" )] = $input->getAttribute ( "value" );
}
// var_dump($inputs);
assert ( array_key_exists ( '__RequestVerificationToken', $inputs ) );
$inputs ['Email'] = 'test1@o2.pl';
$inputs ['Password'] = 'haslo123';
$hc->setopt_array ( array (
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query ( $inputs ),
CURLOPT_URL => 'https://www.prawo-jazdy-360.pl/logowanie' 
) );
$hc->exec ();
hhb_var_dump ( $hc->getResponseHeaders (), $hc->getResponseBody () );

它在最后一行转储已登录的frontpagehtml,证明它已正确登录。

最新更新