2015-10-12 12 views
6

Ho cercato questo sito Web e Google ma non riesco a capire come farlo funzionare. Sto cercando di accedere a popads.net usando uno script PHP, così posso compilare i miei guadagni per i miei siti web su una pagina. Ma questo sito mi sta dando problemi. Qualcuno può vedere cosa sto facendo male?Accedi a PopAds.net

<?php 
 
//username and password of account 
 
$username = 'myusername'; 
 
$password = 'mypassword'; 
 

 
//set the directory for the cookie using defined document root var 
 
$path = DOC_ROOT."/ctemp"; 
 

 
//login form action url 
 
$url="https://www.popads.net/users/login"; 
 
$postinfo = "data[User][username]=".$username."&data[User][password]=".$password; 
 

 
$cookie_file_path = $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); 
 
//set the cookie the site has for certain features, this is optional 
 
curl_setopt($ch, CURLOPT_COOKIE, "cookiename=0"); 
 
curl_setopt($ch, CURLOPT_USERAGENT, 
 
    "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7"); 
 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
 
curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']); 
 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); 
 

 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
 
curl_setopt($ch, CURLOPT_POST, 1); 
 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo); 
 
curl_exec($ch); 
 

 
//page with the content I want to grab 
 
curl_setopt($ch, CURLOPT_URL, "https://www.popads.net/users/dashboard"); 
 
//do stuff with the info with DomDocument() etc 
 
$html = curl_exec($ch); 
 
echo $html; 
 
curl_close($ch);

+0

Scarica WireShark e fare un login normale. Assicurati di catturare la richiesta e riprodurla nel tuo PHP;) – GuyT

risposta

2

EDIT 03/26/2017

Ad un certo punto, PopAds passati ad usare lato client Javascript per generate a check value utilizzando due variabili lato server recuperati da una richiesta AJAX . Sembra abbastanza facile da replicare in PHP, ma dal momento che il JS può essere facilmente modificato, non giochiamo a cat e mouse e usiamo solo un motore per gestire il JS per noi.

Ecco un codice PHP per eseguire uno script CasperJS per accedere e ottenere ciò di cui abbiamo bisogno. Innanzitutto, è necessario installare phpcasperjs/phpcasperjs utilizzando Composer. Avrete anche bisogno di nodejs installati e di installare i seguenti moduli alla directory in cui si prevede di eseguire questo script: npm install phantomjs ; npm install casperjs

<?php 

require_once 'vendor/autoload.php'; 

use Browser\Casper; 

define('POPADS_EMAIL', '[email protected]'); 
define('POPADS_PASS', 'your password'); 

$casper = new Casper(__DIR__ . '/node_modules/casperjs/bin/'); 

//$casper->setOptions(['engine' => 'slimerjs']); 
//$casper->setDebug(true); 

// change the UA! 
$casper->setUserAgent('Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0'); 

// navigate to google web page 
$casper->start('https://www.popads.net/'); 

// wait for text if needed for 3 seconds 
$casper->waitForText('Reset password', 5000); 

//data[User][username] 
//data[User][password] 


$casper->fillFormSelectors(
    'form.didEnabled', 
    array(
     'input#UserUsername' => POPADS_EMAIL, 
     'input#UserPassword' => POPADS_PASS, 
    ), 
    true 
); 

$casper->waitForText('</body>', 5000); 

$casper->capturePage(__DIR__ . '/login.jpg'); 

// run the casper script 
$casper->run(); 

// need to debug? just check the casper output 
//$output = $casper->getOutput(); 

$output = $casper->getHTML(); 

if (strpos($output, 'PopAds - Dashboard') !== false) { 
    echo "Logged in!"; 
} else { 
    echo "Login failed."; 

    var_dump($output); 
} 

Ecco un esempio di lavoro. Ho aggiunto alcune note al codice. Questo non funziona più, lasciato per riferimento.

Il processo di base è:

  • pagina Richiesta del modulo di login
  • Extract "sid" valore simbolico dal modulo di login
  • ingressi forma di estratto
  • input di form compilato con le informazioni di login
  • Invia login su richiesta
  • Estrarre il saldo dalla pagina risultante dopo aver effettuato l'accesso

E il codice:

<?php 

error_reporting(E_ALL);ini_set('display_errors', 1); 

// credentials 
$USERNAME = 'username'; 
$PASSWORD = 'password'; 

// login url 
$LOGINURL = 'https://www.popads.net/users/login'; 

// initialize curl 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); 
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_COOKIEFILE, ''); // empty file means curl will keep cookies for the lifetime of the handle 
// use cookiejar if you'd like to save the cookies for more than the request 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120); 
curl_setopt($ch, CURLOPT_TIMEOUT, 120); 

// set URL and request (establishes cookies, gets login sid) 
curl_setopt($ch, CURLOPT_URL, $LOGINURL); 
$data = curl_exec($ch); 

// look for "sid" value on form action (required) 
preg_match('#/users/login\?sid=([\w\d]+)#', $data, $match); 
$sid  = $match[1]; 

// extract form fields from form 
$formFields = getFormFields($data); 

// set username and password 
$formFields['data[User][username]'] = $USERNAME; 
$formFields['data[User][password]'] = $PASSWORD; 

// build http post string 
$post_string = http_build_query($formFields); 

// update login url with sid value and post login form 
curl_setopt($ch, CURLOPT_URL, $LOGINURL . '?sid=' . $sid); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); 

// execute login request (should be logged in at this point) 
$result = curl_exec($ch); 

// get balance from page 
preg_match('#<h5>Current Balance:</h5>\s*<div class="overview overview_green">(.*?)</div>#is', $result, $balance); 
$balance = trim($balance[1]); 

// show balance 
echo "Your balance is $balance<br>"; 


function getFormFields($data) 
{ 
    if (preg_match('/(<form.*?id=.?UserLoginForm.*?<\/form>)/is', $data, $matches)) { 
     $inputs = getInputs($matches[1]); 

     return $inputs; 
    } else { 
     die('didnt find login form'); 
    } 
} 

function getInputs($form) 
{ 
    $inputs = array(); 

    $elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches); 

    if ($elements > 0) { 
     for($i = 0; $i < $elements; $i++) { 
      $el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]); 

      if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) { 
       $name = $name[1]; 
       $value = ''; 

       if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i', $el, $value)) { 
        $value = $value[1]; 
       } 

       $inputs[$name] = $value; 
      } 
     } 
    } 

    return $inputs; 
} 

uscita:

Il saldo è di $ 0,00

4

Dato che questo sito utilizza la protezione CSRF, al fine di effettuare il login è necessario ottenere prima il token CSRF dalla forma originale e passare questo con i dati di login per l'endpoint di accesso. Il token CSRF ha il nome del campo data[_Token][key] nella home page. Il sito può anche impostare un cookie quando lo imposta in modo tale che dovrai restituire i dati del cookie se lo ricevi da cURL.

Detto questo: la mia raccomandazione è di verificare se dispongono di un'API ufficiale e prima di programmare un raschietto da soli, accertarsi di non applicare alcun termine e condizioni che potrebbero farti inserire nella lista nera.

+0

Ho provato a passare anche il valore dei dati [_Token] [chiave], tuttavia ciò sembra essere statico mentre noto che l'id è ciò che cambia, cosa dovrebbe Sto passando? – user2988034

+0

È necessario passare il cookie che viene impostato di nuovo, cURL non manterrà i cookie per impostazione predefinita; devi catturare l'ID di sessione e restituirlo. – mjsa

1

Add:

# Send previously received cookies. 
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); 

Impostazione CURLOPT_COOKIEJAR salva solo i cookie ricevuti.

Elimina:

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 

Questo duplica la riga successiva in cui si imposta CURLOPT_POST.

Infine, la seconda pagina che si sta scaricando (Dashboard) deve essere recuperata con il normale metodo GET. Aggiungere questo prima del secondo curl_exec() chiamata:

curl_setopt($ch, CURLOPT_HTTPGET, true);