2011-07-19 33 views
0

客戶端重定向這就是我想要做的事:做,捲曲

  • 傳遞形式使用POST變量捲曲聲明=>好
  • 我登錄的網站後去取其頁我想=>不好

這是我現在的腳本:

$postData = array(
    'login' => 'john', 
    'pwd' => 'doe' 
); 

$array_url = array('3109'); 

foreach ($array_url as $k => $i) { 
    echo $i."<br>"; 
    $c = curl_init(); 

    $url = "http://mywebsite/index.php?ok=1"; 

    curl_setopt($c, CURLOPT_URL, $url); 
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($c, CURLOPT_FOLLOWLOCATION, TRUE); 
    curl_setopt($c, CURLOPT_POSTFIELDS, $postData); 

    $output = curl_exec($c); 
    if ($output === false) { 
     trigger_error('Erreur curl : ' . curl_error($c), E_USER_WARNING); 
    } 
    else { 

     var_dump($output); 
     echo $output; 
    } 
    curl_close($c); 
} 

我怎樣才能做到這一點?

編輯:如果我試圖抓取頁面我想諮詢我得到重定向到登錄表單,因爲我無法設置從這個頁面的夫婦登錄/傳遞,我需要獲取checkingpassword一個...

Thx

回答

1

您需要確定遠程頁面如何驗證會話。大多數網站使用cookie來做到這一點。登錄後,任何連續的頁面請求都會通過cookie,遠程站點會檢查cookie,如果它有效並且沒有過期,它會讓您通過,否則,它會將您踢回登錄頁面(或者給您一個錯誤或其他)。

因此,如果它是一個cookie,則需要存儲返回的cookie,並將其用於任何後續的curl請求。

+3

還有了'CURLOPT_COOKIEFILE'&'CURLOPT_COOKIEJAR'選項來對付餅乾。 – hakre

+0

@Anthony確定你是對的,它檢查每個頁面上cookie的phpsessid,所以最後我不需要打擾傳遞登錄表單,我必須檢索服務器端(這是我的網站)的cookie值,並提供它用指令hakre給我捲曲? – krifur

+0

第一個curl請求傳入憑證並傳回cookie。每個下面的curl請求都應該只需要cookie,使用hakre提到的選項。 – Anthony

0

嘗試使用Zend_Http_Client - 它是Zend Framework的一部分。它可以存儲cookie並在每次下一個請求時重新發送它們。下面是例如,從documentation

// To turn cookie stickiness on, set a Cookie Jar 
$client->setCookieJar(); 

// First request: log in and start a session 
$client->setUri('http://example.com/login.php'); 
$client->setParameterPost('user', 'h4x0r'); 
$client->setParameterPost('password', '1337'); 
$client->request('POST'); 

// The Cookie Jar automatically stores the cookies set 
// in the response, like a session ID cookie. 

// Now we can send our next request - the stored cookies 
// will be automatically sent. 
$client->setUri('http://example.com/read_member_news.php'); 
$client->request('GET');