2012-09-08 226 views
0

我正在嘗試使用Curl登錄到https://www.reporo.com/login.php。下面是代碼,我使用的是:它重定向我http://best-payroll-services.info/adder/login.phpCurl https登錄

<?php 
function createPostString($aPostFields) { 
    foreach ($aPostFields as $key => $value) { 
     $aPostFields[$key] = urlencode($key . '=' . $value); 
    } 

    return urlencode(implode('&', $aPostFields)); 
} 

$postFields['username'] = 'login'; 
$postFields['password'] = 'pass'; 
$postFields['submit'] = ' '; 

$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, 'https://www.reporo.com/login.php'); 
curl_setopt($curl, CURLOPT_REFERER, 'https://www.reporo.com/login.php'); 
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13'); 
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/cookies.txt'); 
curl_setopt($curl, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookies.txt'); 
curl_setopt($curl, CURLOPT_POST, TRUE); 
curl_setopt($curl, CURLOPT_POSTFIELDS, createPostString($postFields)); 

curl_exec($curl); 
header('Location: https://www.reporo.com/analytics/dashboard.php'); 
curl_close($curl) 


?> 

使用腳本之後。哪裏有問題 ?

的var_dump(curl_getinfo($ C))之後,我已經有了:

'url' => string 'https://www.reporo.com/login.php' (length=32) 
    'content_type' => string 'text/html' (length=9) 
    'http_code' => int 200 
    'header_size' => int 382 
    'request_size' => int 192 
    'filetime' => int -1 
    'ssl_verify_result' => int 20 
    'redirect_count' => int 0 
    'total_time' => float 0.843 
    'namelookup_time' => float 0 
    'connect_time' => float 0.109 
    'pretransfer_time' => float 0.531 
    'size_upload' => float 255 
    'size_download' => float 3233 
    'speed_download' => float 3835 
    'speed_upload' => float 302 
    'download_content_length' => float 3233 
    'upload_content_length' => float 255 
    'starttransfer_time' => float 0.655 
    'redirect_time' => float 0 
    'certinfo' => 
    array 
     empty 
    'redirect_url' => string '' (length=0) 

問候。

+0

請問你到底用什麼代碼實現?如果登錄是正確的,您還希望重定向到哪裏? – tftd

+0

我試圖登錄到reporo.com,但它不起作用;/ –

+0

所以,如果我理解你是正確的,你有一個腳本(運行在不同的服務器上),應該檢查你是否有cookies。如果不是,腳本會將登錄後數據發送到遠程腳本(位於@ reporo.com),然後重定向。那是對的嗎? – tftd

回答

2

有一個這個腳本錯了幾件事。

  • 您需要餅乾的相對路徑更改:

    if (file_exists('cookies.txt')) 
    

    到絕對

    if (file_exists(dirname(__FILE__).'/cookies.txt')) 
    

    讓你確定它的檢查正確的文件。

  • 另外str_replace('/', '', $_SERVER[ "PHP_SELF" ]);是一個返回函數。這意味着你需要存儲的返回值:

    $redirect_url = str_replace('/', '', $_SERVER[ "PHP_SELF" ]); 
    header('Location:'.$redirect_url); 
    

我你的代碼稍加修改,以這樣的:

function connect($url, $post = '') 
{ 
    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_URL, $url); 
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.100 Safari/534.30'); 
    curl_setopt($curl, CURLOPT_HEADER, 0); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0); 
    curl_setopt($curl, CURLOPT_POST, 1); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($curl, CURLOPT_COOKIEFILE, __DIR__.'/cookies.txt'); 
    curl_setopt($curl, CURLOPT_COOKIEJAR, __DIR__.'/cookies.txt'); 
    $result = curl_exec($curl); 
    curl_close($curl); 
    return $result; 
} 

$login_data[ 'username' ] = $_POST['login']; 
$login_data[ 'password' ] = $_POST['password']; 

foreach ($login_data as $key => $value) { 
    $post_items[ ] = $key.'='.$value; 
} 
$post_string = implode('&', $post_items); 

if (file_exists(__DIR__.'/cookies.txt')) { 
    // Cookies exist, point to a secured page? 
    echo connect('https://yourhost/secure.php') 
} 
else { 
    // Cookies don't exist post to a login page and redirect to a secured page? 
    connect('https://yourhost/login.php', $post_string) 
    $redirect_url = str_replace('/', '', $_SERVER[ "PHP_SELF" ]); 
    header('Location: '.$redirect_url); 

} 
+0

我用你的代碼,它仍然不工作,我有信息:請再試一次:請登錄,當我登錄後重定向到網站:https://reporo.com/analytics/dashboard.php,所以evererything是好的,但它不會登錄; /;) –

+0

我開始懷疑你不太清楚你在做什麼,或者你根本無法解釋它。這個腳本是用來遠程登錄到另一個站點,還是用它來登錄到你自己的站點? – tftd

+0

它登錄到另一個不是我的網站,實際上僅此而已;) –

1

我假設它重定向到該網站屬於你,因爲在那個代碼,重定向的唯一事情是以下行:

header('Location: '.$_SERVER[ "PHP_SELF" ]); 

我猜問題是:

if (file_exists('cookies.txt')) 

該文件似乎不存在的,這就是爲什麼它的運行:

connect('https://www.reporo.com/login.php', $post_string); 
str_replace('/', '', $_SERVER[ "PHP_SELF" ]); 
header('Location: '.$_SERVER[ "PHP_SELF" ]); 
+0

cookie.txt的存在,並且有:.www.reporo.com \t \t FALSE/\t \t FALSE 0 \t \t PHPSESSID 8fb5f0016b3eb25c9dd23856acdfe47f –

+0

'file_exists( 'cookie.txt的')'返回真?如果PHP重定向您,它不會返回true。 – NightHawk

+0

在問題文章中,我編輯了代碼,但它仍然不起作用;/Cookies路徑是正確的。我已經檢查過了。 –

1

這裏有一個腳本,我寫登錄到Reporo網站和下載特定日期的總體廣告客戶統計信息(在網址中指定)。我很確定這是你要找的。

// Login & download stats in CSV format 
function get_stats($login_url, $username, $password, $login_button, $csv_title, $download_url){ 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $login_url); 
    $post_fields = 'username='.urlencode($username).'&password='.urlencode($password); 
    // Some sites don't send this variable, hence the check 
    if(trim(urlencode($login_button)) != ''){ 
     $post_fields .= '&'.$login_button.'='.$login_button; 
    } 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/cookies.txt'); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookies.txt'); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3"); 
    curl_exec($ch); 

    // Prevent the script from timing out 
    set_time_limit(0); 

    $fp = fopen (dirname(__FILE__) . '/'.$csv_title, 'w+');//This is the file where we save the information 
    curl_setopt($ch, CURLOPT_URL, $download_url);//Here is the file we are downloading 
    curl_setopt($ch, CURLOPT_TIMEOUT, 50); 
    curl_setopt($ch, CURLOPT_FILE, $fp); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
    curl_exec($ch); 
    curl_close($ch); 
    fclose($fp); 
} 

// Set REPORO variables 
$rep__login_url = 'https://www.reporo.com/login.php'; 
$rep_un = 'USERNAME'; 
$rep_pw = 'PASSWORD'; 
$rep_login_butt = 'submit'; 
$rep_csv = 'reporo_stats.csv'; 
// In the url, list= YOUR CAMPAIGN IDS (comma seperated) 
$rep_dl_url = 'https://www.reporo.com/analytics/data.php?type=AdvertiserEntity&start=2012-10-17&end=2012-10-17&entity=client&list=1234,5678,9101,1121,3141&format=csv'; 

// Call get_stats function 
get_stats($rep__login_url, $rep_un, $rep_pw, $rep_login_butt, $rep_csv, $rep_dl_url);