2010-06-10 48 views
3

登錄需要您登錄的網站時,我一直無法下載圖片以供下載。圖片只能在您登錄網站時查看,但如果你將它的位置複製到標籤頁/新窗口(它重定向到錯誤頁面 - 所以我猜包含文件夾是.htaccess-ed),你似乎無法直接在瀏覽器中查看它們。保存圖片只有在登錄後纔可用

無論如何,我下面的代碼允許我登錄並獲取HTML內容,這很有效 - 但我無法抓取圖像......這是我需要幫助的地方!

<? 
// curl.php 

class Curl { 

    public $cookieJar = ""; 

    public function __construct($cookieJarFile = 'cookies.txt') { 
     $this->cookieJar = $cookieJarFile; 
    } 

    function setup() { 
     $header = array(); 
     $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,"; 
     $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/gif;q=0.8,image/x-bitmap;q=0.8,image/jpeg;q=0.8,image/png,*/*;q=0.5"; 
     $header[] = "Cache-Control: max-age=0"; 
     $header[] = "Connection: keep-alive"; 
     $header[] = "Keep-Alive: 300"; 
     $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"; 
     $header[] = "Accept-Language: en-us,en;q=0.5"; 
     $header[] = "Pragma: "; // browsers keep this blank. 

     curl_setopt($this->curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7'); 
     curl_setopt($this->curl, CURLOPT_HTTPHEADER, $header); 
     curl_setopt($this->curl, CURLOPT_COOKIEJAR, $this->cookieJar); 
     curl_setopt($this->curl, CURLOPT_COOKIEFILE, $this->cookieJar); 
     curl_setopt($this->curl, CURLOPT_AUTOREFERER, true); 
     curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true); 
     curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); 
    } 

    function get($url) { 
     $this->curl = curl_init($url); 
     $this->setup(); 

     return $this->request(); 
    } 

    function getAll($reg, $str) { 
     preg_match_all($reg, $str, $matches); 
     return $matches[1]; 
    } 

    function postForm($url, $fields, $referer = '') { 
     $this->curl = curl_init($url); 
     $this->setup(); 
     curl_setopt($this->curl, CURLOPT_URL, $url); 
     curl_setopt($this->curl, CURLOPT_POST, 1); 
     curl_setopt($this->curl, CURLOPT_REFERER, $referer); 
     curl_setopt($this->curl, CURLOPT_POSTFIELDS, $fields); 
     return $this->request(); 
    } 

    function getInfo($info) { 
     $info = ($info == 'lasturl') ? curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL) : curl_getinfo($this->curl, $info); 
     return $info; 
    } 

    function request() { 
     return curl_exec($this->curl); 
    } 
} 

?> 

以下是使用它的頁面。

<? 
// data.php 

include('curl.php'); 
$curl = new Curl(); 

$url = "http://domain.com/login.php"; 
$newURL = "http://domain.com/go_here.php"; 

$username = "user"; 
$password = "pass"; 

$fields = "user=$username&pass=$password"; 

// Calling URL 
$referer = "http://domain.com/refering_page.php"; 

$html = $curl->postForm($url, $fields, $referer); 

$html = $curl->get($newURL); 
echo $html; 

?> 

我試圖把對圖像到$的newURL直接URL,但不獲取圖像 - 它只是返回一個錯誤說,因爲該文件夾是不可用直接查看。我試過用不同的方法改變上面的內容,但是我沒有成功獲取圖像,儘管我已經設法通過基本上說錯誤405和/或406(但不是我想要的圖像)來獲得屏幕。

任何幫助將是偉大的!

回答

0

哇,

看起來像複雜的問題。

我會做的是將瀏覽器會話與HTTP層的PHP代碼進行比較,看看有什麼不同。

抓取Wireshark,使用您的瀏覽器成功連接。您需要過濾掉所有其他流量,並只轉儲端口80上的內容。如果右鍵單擊數據包並單擊「遵循TCP流」,它會爲您提供HTTP標頭和頁面輸出。

然後執行相同的操作,但是這次使用PHP腳本。

然後比較標題,看看有什麼不同。也許你缺少一個或兩個標題,也許你需要先去一個頁面,也許你的PHP腳本沒有發送正確的cookies。

0

從網站的行爲看來,它不是會話(cookie)問題,否則打開另一個選項卡將允許您下載圖像。

檢查http referrer,它是我列表中的第一個嫌疑人。

相關問題