нет ответа при входе на сайт через php http - PullRequest
0 голосов
/ 08 марта 2019

Я пытаюсь автоматизировать вход на веб-сайт с помощью веб-запроса с использованием PHP.Однако у меня возникла проблема, так как я просто возвращаю страницу входа без успешного входа в систему.

Веб-сайт, на который я пытаюсь войти: http://textmmo.tk

Библиотека http, которую я использую: https://github.com/divinity76/hhb_.inc.php/blob/master/hhb_.inc.php

мой код:

<?php

$myusername = "Panda";
$mypassword = "letmein1";
error_reporting(E_ALL);

include("hhb_inc.php");

$hc = new hhb_curl('', true);

//
//now to fetch the csrf token and cookies
//

$json = $hc->setopt_array(array(
        CURLOPT_URL => 'http://textmmo.tk/',/*
        CURLOPT_COOKIEJAR => 'LandingCookies.txt',
        CURLOPT_COOKIEFILE => 'LandingCcookies.txt'*/
    ))->exec()->getStdOut();

    $parsed = json_decode($json, true);

    $cookies_raw = $hc->getResponseHeaders();
    $cookies_parsed = array();
    foreach ($cookies_raw as $tmp) {
        if (false === strpos($tmp, ':')) {
            // don't need this header
            continue;
        }
        $tmp = explode(':', $tmp, 2);
        $cookies_parsed[trim($tmp[0])] = trim($tmp[1]);
    }

    //hhb_var_dump($cookies_parsed); 

    // grab token from hidden input type
    $csrftoken_string = $cookies_parsed["Set-Cookie"];
    $csrftoken_imploded = explode(";", $csrftoken_string);
    $csrftoken_full = $csrftoken_imploded[0];
    $csrftoken_peice = explode("=", $csrftoken_full);
    $csrftoken = $csrftoken_peice[1];

    // build login request now

    $myval = 'value="';
    $myval2 = '"';
    $myhtml_imploded = explode($myval, $json);
    $myhtml_full = $myhtml_imploded[1];
    $myhtml_peice = explode($myval2, $myhtml_full);
    $csrfmiddlewaretoken = $myhtml_peice[0];


    /*print "csrfmiddlewaretoken $csrfmiddlewaretoken";
    print "\n$myusername";
    print "\n$mypassword\n \n \n \n";*/

    $mypostfields = array('csrfmiddlewaretoken' => $csrfmiddlewaretoken, 'username' => $myusername, 'password' => $mypassword);

    $twofactorHeaders = array(
          //  'Connection: keep-alive',
            'Content-Type: application/x-www-form-urlencoded',
     //       'Cache-Control: max-age=0',
            'Origin: http://textmmo.tk',
       //     'Upgrade-Insecure-Requests: 1',
            'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'Access-Control-Request-Headers: content-type',
      //      'Referer: http://textmmo.tk/accounts/login/',
     //       'Accept-Encoding: gzip, deflate',
    //        'Accept-Language: en-US,en;q=0.9',            
         //  'Content-Length: ' . sizeof($mypostfields),
        );

    //hhb_var_dump($twofactorHeaders) & die();
    $json2 = $hc->setopt_array(array(
        CURLOPT_URL => "http://textmmo.tk/accounts/login",
        CURLOPT_POST => 1,
        CURLOPT_FOLLOWLOCATION => TRUE,
        CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
        CURLOPT_POSTFIELDS => $mypostfields,
        CURLOPT_HTTPHEADER => $twofactorHeaders
    ))->exec()->getStdOut();
    $parsed = json_decode($json2, true);

    print $json2 . ">>"; exit();

?>
...