2015-10-12 15 views
6

Tôi đã tìm kiếm trang web này và google nhưng dường như không thể tìm ra cách để làm việc này. Tôi đang cố gắng đăng nhập vào popads.net bằng cách sử dụng một tập lệnh PHP để tôi có thể biên dịch thu nhập của mình cho các trang web của tôi trên một trang. Nhưng trang web này đang gây rắc rối cho tôi. Ai có thể nhìn thấy những gì tôi đang làm sai?Đăng nhập vào 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

Tải Wireshark và làm một đăng nhập thường xuyên. Hãy chắc chắn rằng bạn nắm bắt yêu cầu và phát lại nó trong PHP của bạn;) – GuyT

Trả lời

2

EDIT 03/26/2017

Tại một số điểm, PopAds chuyển sang sử dụng phía client Javascript để generate a check value sử dụng hai biến phía máy chủ lấy ra từ một yêu cầu AJAX . Nó có vẻ dễ dàng, đủ để nhân rộng trong PHP nhưng kể từ khi JS có thể dễ dàng thay đổi, chúng ta không chơi mèo và chuột và chỉ cần sử dụng một công cụ để xử lý JS cho chúng ta.

Đây là một số mã PHP để chạy một kịch bản CasperJS để đăng nhập và nhận được những gì chúng ta cần. Trước tiên, bạn cần phải cài đặt phpcasperjs/phpcasperjs bằng cách sử dụng Composer. Bạn cũng sẽ cần nodejs cài đặt, và cài đặt các module sau vào thư mục nơi bạn có kế hoạch để chạy kịch bản này: 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); 
} 

Dưới đây là một ví dụ làm việc. Tôi đã thêm một số ghi chú vào mã. Điều này không còn hoạt động, để lại để tham khảo.

Quá trình cơ bản là:

  • Yêu cầu trang biểu mẫu đăng nhập
  • Extract "sid" giá trị token từ mẫu đăng nhập
  • dạng Extract đầu vào
  • hình thức cư đầu vào với thông tin đăng nhập của bạn
  • Gửi yêu cầu đăng nhập
  • Trích xuất số dư từ trang kết quả sau khi đăng nhập

Và mã:

<?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; 
} 

Output:

cân bằng của bạn là $ 0,00

4

Là trang web này đang sử dụng bảo vệ CSRF, để đăng nhập đầu tiên bạn cần có được CSRF token từ hình thức ban đầu và chuyển thông tin này với các dữ liệu đăng nhập vào thiết bị đầu cuối đăng nhập. Mã thông báo CSRF có tên trường data[_Token][key] trên trang chủ. Trang web cũng có thể đặt cookie khi thiết lập cookie này, vì vậy bạn sẽ cần phải chuyển dữ liệu cookie đó trở lại nếu bạn nhận được nó từ cURL.

Điều này cho biết: đề xuất của tôi là xem họ có API chính thức hay không và trước khi tự viết mã, hãy đảm bảo bạn không breeching bất kỳ điều khoản và điều kiện nào có thể khiến bạn bị đưa vào danh sách đen.

+0

Tôi đã cố gắng chuyển giá trị của dữ liệu [_Token] [key], tuy nhiên điều đó có vẻ là tĩnh trong khi tôi nhận thấy id là những gì thay đổi, những gì nên Tôi đi qua? – user2988034

+0

Bạn cần phải chuyển cookie được đặt lại, cURL sẽ không giữ cookie theo mặc định; bạn phải nắm bắt ID phiên và chuyển nó trở lại. – mjsa

1

Add:

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

Thiết CURLOPT_COOKIEJAR chỉ tiết kiệm cookie nhận.

Xóa:

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

này bản sao dòng tiếp theo, nơi bạn thiết lập CURLOPT_POST.

Cuối cùng, trang thứ hai bạn đang tải xuống (Trang tổng quan) cần được tìm nạp bằng phương thức GET thông thường. Thêm này trước thứ hai curl_exec() gọi:

curl_setopt($ch, CURLOPT_HTTPGET, true);