Php CURL登录后prawo-jazdy-360.pl

Php CURL登录后prawo-jazdy-360.pl,php,curl,post,login,Php,Curl,Post,Login,我无法登录此网站 不要手动设置内容类型:application/x-www-form-urlencoded标题,让curl为您设置。(参见curl_setopt docs关于CURLOPT_POSTFIELDS) 不要手动设置content-length标题,让curl为您设置 不要做curl_setopt($ch,CURLOPT_CUSTOMREQUEST,“POST”),只需设置CULLOPT_POST 您设置了内容类型:application/x-www-form-urlencoded,但

我无法登录此网站

不要手动设置
内容类型:application/x-www-form-urlencoded
标题,让curl为您设置。(参见curl_setopt docs关于CURLOPT_POSTFIELDS)

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

不要做
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,“POST”),只需设置CULLOPT_POST

您设置了
内容类型:application/x-www-form-urlencoded
,但您的$postInfo甚至不接近urlencoded格式。事实上,它看起来更像JSON编码,但即使它是JSON,$username/$password也没有正确地进行JSON编码(它的做法类似于
{username:foo}
,而不是
{username:foo}
),所以idk知道这是什么编码,但它肯定不是编码,告诉服务器它是(不是x-www-form-urlencoded格式)。另外,创建正确的x-www-form-urlencoded字符串的最简单方法是使用
http\u build\u query
函数

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

解决这些问题,然后再试一次。不要将其发送为json编码,而是将其发送为
application/x-www-form-urlencoded
,这是登录页面使用的

使用hhb_curl from,下面是一个工作示例代码:

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

<?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 () );