2010-06-10 50 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,它是我列表中的第一个嫌疑人。

相关问题