2015-09-25 2 views
1

Я пытаюсь создать скрипт, который входит в мою учетную запись Linkbucks, чтобы получить текущую статистику.cURL войти в Linkbucks

Они обеспечивают и api, но только для создания ссылок, мне нужно, чтобы вы получали статистику.

Вещи, которые я обнаружил:

  1. Во-первых, вы должны оставаться зарегистрированным
  2. Чтобы получить статистику, веб-сайт делает Ajax вызов: https://www.linkbucks.com/Profile.aspx?task=manageLinks&action=loadPublisherStats с JSON пост, как это: { «месяц»: «09/01/2015»}.

С этим сообщением легко получить необходимую информацию, проблема в том, что мой скрипт не работает.

Я делюсь с вами кодом, поэтому, пожалуйста, помогите мне.

Любая идея или решение, или что-то еще будет оценено.

Вот мой сценарий:

<?php 

$urlLogin = "https://www.linkbucks.com/Default.aspx"; 

$ch = getSource($urlLogin); 
$fuente = curl_exec($ch); 

$re = "/<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"(.*?)\" \\/>/"; 
preg_match($re, $fuente, $matches); 

$re = "/<input type=\"hidden\" name=\"__EVENTVALIDATION\" id=\"__EVENTVALIDATION\" value=\"(.*?)\" \\/>/"; 
preg_match($re, $fuente, $validation); 

$re = "/<input type=\"hidden\" name=\"__VIEWSTATEGENERATOR\" id=\"__VIEWSTATEGENERATOR\" value=\"(.*?)\" \\/>/"; 
preg_match($re, $fuente, $generator); 

$post = array(
    "ctl00\$ctl00\$phMenu\$LeftMenuBar\$ctl00\$Username" => "yourusername" , 
    "ctl00\$ctl00\$phMenu\$LeftMenuBar\$ctl00\$Password" => "yourpassword" , 
    "__VIEWSTATE" => $matches[1] , 
    "__VIEWSTATEGENERATOR" => $generator[1] , 
    "__EVENTVALIDATION" => $validation[1] 
); 
$data = postData($urlLogin, $post); 

echo $data; 

function getSource($url, $header = null) { 
    $ch = curl_init($url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
    $config['useragent'] = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.33 (KHTML, like Gecko) Ubuntu/9.10 Chromium/13.0.752.0 Chrome/13.0.752.0 Safari/534.33'; 
    curl_setopt($ch, CURLOPT_USERAGENT, $config['useragent']); 
    curl_setopt($ch, CURLOPT_REFERER, (is_null($header) ? 'https://www.google.com/' : $header)); 
    return $ch; 
} 

function postData($url , $array) { 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array)); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); 

    curl_setopt($ch, CURLOPT_REFERER, "https://www.linkbucks.com/Default.aspx"); 

    $server_output = curl_exec($ch); 


    curl_setopt($ch, CURLOPT_URL, 'https://www.linkbucks.com/Default.aspx?ReturnUrl=%2fManageLinks'); 
    curl_setopt($ch, CURLOPT_POST, 0); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
    $server_output = curl_exec($ch); 

    return ($server_output); 
} 

?> 
+0

Какую ошибку вы получаете? – enkrates

+0

У меня не было никаких ошибок, просто код не работает, но теперь решено !! – Alberto

ответ

0

Наконец, благодаря runz0rd я был в состоянии сделать тыс e код работа !!

Благодарю вас, ребята!

Вот окончательный вариант кода:

<?php 

$urlLogin = "https://www.linkbucks.com/Default.aspx?ReturnUrl=%2fManageLinks"; 
$useragent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.33 (KHTML, like Gecko) Ubuntu/9.10 Chromium/13.0.752.0 Chrome/13.0.752.0 Safari/534.33'; 
$source = getSource($urlLogin, $useragent); 

$viewstate = findStringByRegex("/<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"(.*?)\" \/>/", $source); 
$generator = findStringByRegex("/<input type=\"hidden\" name=\"__VIEWSTATEGENERATOR\" id=\"__VIEWSTATEGENERATOR\" value=\"(.*?)\" \\/>/", $source); 
$validation = findStringByRegex("/<input type=\"hidden\" name=\"__EVENTVALIDATION\" id=\"__EVENTVALIDATION\" value=\"(.*?)\" \\/>/", $source); 


$post = array(
    'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$Username' => "yourusername", 
    'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$Password' => "yourpassword", 
    'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$LoginImgBtn.x' => 0, 
    'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$LoginImgBtn.y' => 0, 
    "__VIEWSTATE" => $viewstate, 
    "__VIEWSTATEGENERATOR" => $generator, 
    "__EVENTVALIDATION" => $validation 
); 
$data = postData($urlLogin, $post); 

echo $data; 

function getSource($url, $useragent = null, $header = null) { 
    $ch = curl_init($url); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_USERAGENT, $useragent); 
    curl_setopt($ch, CURLOPT_REFERER, (is_null($header) ? 'https://www.google.com/' : $header)); 

    return curl_exec($ch); 
} 

function postData($url, $postArray) { 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postArray)); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); 
    curl_setopt($ch, CURLOPT_REFERER, "https://www.linkbucks.com/Default.aspx"); 

    $server_output = curl_exec($ch); 

    curl_setopt($ch, CURLOPT_URL, "https://www.linkbucks.com/Profile.aspx?task=manageLinks&action=loadPublisherStats"); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); 
    $fecha = json_encode(array("month"=> "09/01/2015")); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fecha); 
    $server_output = curl_exec($ch); 

    return $server_output; 
} 

function findStringByRegex($regex, $source) { 
    preg_match($regex, $source, $matches); 
    return $matches[1]; 
} 

?> 
1

Это должно работать:

$urlLogin = "https://www.linkbucks.com/Default.aspx?ReturnUrl=%2fManageLinks"; 
$useragent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.33 (KHTML, like Gecko) Ubuntu/9.10 Chromium/13.0.752.0 Chrome/13.0.752.0 Safari/534.33'; 
$source = getSource($urlLogin, $useragent); 

$viewstate = findStringByRegex("/<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"(.*?)\" \/>/", $source); 
$generator = findStringByRegex("/<input type=\"hidden\" name=\"__VIEWSTATEGENERATOR\" id=\"__VIEWSTATEGENERATOR\" value=\"(.*?)\" \\/>/", $source); 
$validation = findStringByRegex("/<input type=\"hidden\" name=\"__EVENTVALIDATION\" id=\"__EVENTVALIDATION\" value=\"(.*?)\" \\/>/", $source); 


$post = array(
    'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$Username' => "yourUsername", 
    'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$Password' => "yourPassword", 
    'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$LoginImgBtn.x' => 0, 
    'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$LoginImgBtn.y' => 0, 
    "__VIEWSTATE" => $viewstate, 
    "__VIEWSTATEGENERATOR" => $generator, 
    "__EVENTVALIDATION" => $validation 
); 
$data = postData($urlLogin, $post); 

echo $data; 

function getSource($url, $useragent = null, $header = null) { 
    $ch = curl_init($url); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_USERAGENT, $useragent); 
    curl_setopt($ch, CURLOPT_REFERER, (is_null($header) ? 'https://www.google.com/' : $header)); 

    return curl_exec($ch); 
} 

function postData($url, $postArray) { 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postArray); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); 
    curl_setopt($ch, CURLOPT_REFERER, "https://www.linkbucks.com/Default.aspx"); 

    $server_output = curl_exec($ch); 

    return $server_output; 
} 

function findStringByRegex($regex, $source) { 
    preg_match($regex, $source, $matches); 

    return $matches[1]; 
} 

Ive очистить ваш код немного, это было в основном хорошо, вы просто не хватает 2 почтовых полей: ctl00 $ ctl00 $ phMenu $ LeftMenuBar $ ctl00 $ LoginImgBtn.x и ctl00 $ ctl00 $ phMenu $ LeftMenuBar $ ctl00 $ LoginImgBtn.y»

+0

Спасибо вам большое! Я завершил его с информацией POST, чтобы получить ответ JSON, и он работает нормально. Я очень ценю это, действительно, спасибо :) – Alberto

+0

Нет проблем, просто отметьте вопрос как ответ;) – runz0rd

Смежные вопросы