2017-08-17 174 views
0

我正在嘗試使用curl php檢查網站的登錄信息。 請求頭看起來是這樣的:如何在請求頭中獲取Cookie,同時捲曲php

GET /loginpage.cgi HTTP/1.1 
Host: test.com 
Connection: keep-alive 
pragma: no-cache 
Cache-Control: no-cache, no-store, max-age=0, must-revalidate 
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36 
Expires: Fri, 01 Jan 1990 00:00:00 GMT 
Accept: */* 
Referer: https://test.com/login.cgi 
Accept-Encoding: gzip, deflate, br 
Accept-Language: en-US,en;q=0.8 
Cookie: QSESSIONID=**c0da73be1917324c157e5c45b1bccd4f** 

的QSESSIONID值的每一次變化,我需要這個值是在頭道,以使從PHP的捲曲請求。我的php代碼如下:

<?php 

$username = 'user'; 
$password = 'pass'; 

$url = "https://test.com/login.cgi?key=GetLoginUserInfo"; 

$ch = curl_init(); 
$headers = array(); 
$headers[] = "Pragma: no-cache"; 
$headers[] = "Accept-Encoding: gzip, deflate, br"; 
$headers[] = "Accept-Language: en-US,en;q=0.8"; 
$headers[] = "User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) 
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"; 
$headers[] = "Accept: */*"; 
$headers[] = "Referer: https://test.com/login.cgi?key=UDIT"; 
$headers[] = "Cookie: QSESSIONID=c0da73be1917324c157e5c45b1bccd4f"; 
$headers[] = "Connection: keep-alive"; 
$headers[] = "Cache-Control: no-cache, no-store, max-age=0, must- 
revalidate"; 
$headers[] = "Expires: Fri, 01 Jan 1990 00:00:00 GMT"; 

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); 
$result = curl_exec($ch); 

print_r($result); 
$ch_error = curl_error($ch); 

if ($ch_error) { 
    echo "cURL Error: $ch_error"; 
} else { 
    echo $result; 
} 

curl_close($ch); 
?> 

現在,當我運行這個。當我從開發工具請求標頭獲取會話標識並將其更改爲標題時,只有我可以登錄,因爲每次登錄時都會更改該值。 enter image description here

所以我的問題是,他們是一種方式,我可以從請求頭獲取該值,以便我可以在PHP代碼中的頭中追加該值。或者您可以建議的任何其他方式,我很樂意提供建議。 如果我從php代碼中刪除此信息,請求將失敗,如果

回答

0

這看起來像會話ID(名稱有點不同),它們會在您的第一個連接上生成。你應該存儲這個,並將其作爲一個cookie發送到後續的請求中,以便服務器可以跟蹤你的會話。

如果你不發送這個cookie頭,你會得到一個隨機的每個請求。

此代碼的寫入方式試圖教您如何處理cookie。對於你想要做什麼有很多假設,但是這給你一個關於如何解析/處理標題和會話ID的基本知識。

我已經避免使用cookiejar(它更容易和更清晰的代碼),因爲它會自動爲您做所有這些,我建議您在學習會話ID的工作方式時仔細研究它們。

<?php 

class MyService 
{ 

    private $headers = []; 
    private $cookies = []; 
    private $loggedIn = false; 


    function login($username, $password) 
    { 
     $ch = curl_init('https://test.com/login.cgi'); 

     #Assumption: Do whatever is needed for login here 
     curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password); 

     # This is where we setup header processing 
     curl_setopt($ch, CURLOPT_HEADERFUNCTION, [$this, 'parseHeaders']); 

     #Assumption: Check for valid response 
     $response = curl_exec($ch); 

     $this->loggedIn = ($response == 'true'); 

     curl_close($ch); 

     return $this->loggedIn; 
    } 


    function getHeader($header) 
    { 
     return array_key_exists($header, $this->headers) ? $this->headers[$header] : null; 
    } 

    function getCookie($cookie) 
    { 
     return array_key_exists($cookie, $this->cookies) ? $this->cookies[$cookie] : null; 
    } 

    function parseHeaders($ch, $header) 
    { 
     if (stristr($header, 'set-cookie')) { 

      # If you can install PECL pecl_http this will work better 
      // $this->cookies = http_parse_cookie(strstr('Set-Cookie', $header))['cookies']; 

      # Otherwise 

      $reserved_words = [ 
       'httponly', 
       'expire', 
       'path', 
       'expires', 
       'domain', 
       'secure' 
      ]; 

      preg_match("/Set-Cookie: (.*)/", $header, $cookies); 

      foreach ($cookies as $cookie) { 
       $cookie = explode(';', $cookie); 

       foreach ($cookie as $cookie_part) { 

        $cookie_part = explode('=', $cookie_part); 

        array_walk($cookie_part, create_function('&$val', '$val = trim($val);')); 

        if (!in_array($cookie_part[0], $reserved_words) && isset($cookie_part[1])) { 
         $this->cookies[$cookie_part[0]] = $cookie_part[1]; 
        } 
       } 
      } 

     } else { 

      $header_part = explode(':', $header, 2); 

      if (isset($header_part[1])) { 
       $this->headers[trim($header_part[0])] = trim($header_part[1]); 
      } 
     } 

    } 

    function otherInfo() 
    { 

     if (!$this->loggedIn) { 
      throw new NotLoggedInException('Login first'); 
     } 

     $headers = []; # Populate whatever headers are mandatory 

     $url = "https://test.com/login.cgi?key=GetOtherInfo"; 

     $ch = curl_init(); 

     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
     curl_setopt($ch, CURLOPT_URL, $url); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

     curl_setopt($ch, CURLOPT_COOKIE, 'QSESSIONID=' . $this->getCookie('QSESSIONID')); 

     $result = curl_exec($ch); 

     curl_close($ch); 

     return $result; 

    } 

    function getUserInfo() 
    { 

     if (!$this->loggedIn) { 
      throw new NotLoggedInException('Login first'); 
     } 

     $headers = []; # Populate whatever headers are mandatory 

     $url = "https://test.com/login.cgi?key=GetLoginUserInfo"; 

     $ch = curl_init(); 

     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
     curl_setopt($ch, CURLOPT_URL, $url); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

     curl_setopt($ch, CURLOPT_COOKIE, 'QSESSIONID=' . $this->getCookie('QSESSIONID')); 

     $result = curl_exec($ch); 

     curl_close($ch); 

     return $result; 

    } 

} 

$username = 'user'; 
$password = 'pass'; 

$api = new MyService(); 
$api->login($username, $password); 

$info = $api->getUserInfo(); 
$other = $api->otherInfo(); 
+0

餵你好,我仍然無法獲取數據。爲了讓我的問題更清楚,我試圖獲取這個頁面上的數據,看起來像這樣,{「success」:「true」,「data」:{「UserId」:「user」,「LoginAccess」:「 READ-ONLY「,」ActiveUsers「:」1「,」UserLevel「:」3「},」message「:」「},但我得到字符串(60)」{「success」:「false」 「:)」,「message」:「Session Expired」} –

+0

但是我無法登錄,因爲會話ID。我以前從未使用會話,所以我不知道該怎麼做。 –

+0

隨着您的第一個請求你會得到一個頭部設置一些餅乾,它看起來像這樣: 'Set-Cookie:QSESSIONID = c0da73be1917324c157e5c45b1bccd4f;' 你需要把它,然後設置爲一個cookie頭用於系統的其他請求,包括登錄。 – Ollie