2012-09-05 15 views
2

我做了一個很好的簡單userscript:
當我瀏覽網頁,我可以「書籤」的任何圖像在1點擊
我userscript錯誤與捲曲腳本複製IMG(PHP)時

  1. 抓鬥在IMG SRC
  2. 抓取網頁
  3. 複製的URL .JPG巴紐.gif若要我的服務器

一切都運行完美LY,但在某些情況下,腳本無法複製文件...
實際上,文件被創建,但不包含IMG數據,它只包含一個錯誤網頁的內容:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> 
<html><head> 
<title>403 Forbidden</title> 
</head><body> 
<h1>Forbidden</h1> 
<p>You don't have permission to access /data/x/xxx_xxx_x.jpg on this server.</p> 
<p>Additionally, a 403 Forbidden 
error was encountered while trying to use an ErrorDocument to handle the request.</p> 
<hr> 
<address>Apache Server at xxxxxxxx.net Port 80</address> 
</body></html> 

「複製」代碼(PHP):

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $urlimg); 
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
set_time_limit(300); # 5 minutes for PHP 
curl_setopt($ch, CURLOPT_TIMEOUT, 300); # and also for CURL 
$path = $dirpix.'/'.$aa.'/'.$mm; 
if (! is_dir($path)) { 
    mkdir($path); 
} 
$outfile = fopen($path.'/'.$id.'.'.$ext, 'wb'); 
curl_setopt($ch, CURLOPT_FILE, $outfile); 
curl_exec($ch); 
fclose($outfile); 
curl_close($ch); 

也許網站封鎖那種「複製」腳本? 謝謝!

+1

您是否檢查了返回錯誤消息的網站的政策? 這聽起來像它會是某種[盜鏈預防](http://blog.mark8t.com/2009/02/07/hotlink-prevention-how-to-stop-people-from-linking-to - 你的圖像/)到位。 – mariosangiorgio

+0

nope,我可以熱鏈接,沒問題(我的腳本的第一個版本沒有複製文件,我只是用hotlinks) – jrm

回答

1

兩件事我能想到的這裏,

  1. 設置一個用戶代理在捲曲的請求。因爲根據你的說法,你可以查看圖像,但curl得到了403錯誤,很可能是服務器端的userAgent過濾。

  2. 將referer添加到您的捲髮請求中。您可以將引用者信息從您的腳本發送到php腳本。你必須發佈或得到window.location.href的價值。

+0

有趣...看起來很聰明 - 我會盡力做到這一點,並讓你張貼,謝謝 – jrm

+0

很難知道它是否會改變任何事情,因爲我無法複製錯誤......但我在腳本中添加了您的非常好的創意 - 謝謝! – jrm

+0

不客氣。也許你也可以看看另一個聰明的解決方案:將圖像轉換爲base64字符串並將其發送到您的服務器。請參閱http://stackoverflow.com/questions/6150289/how-to-convert-image-into-base64-string-using-javascript – Prasanth

0

試試看下面的代碼它在我的服務器上正常工作。它是測試代碼: -

<?php 

$img[]='http://i.indiafm.com/stills/celebrities/sada/thumb1.jpg'; 
$img[]='http://i.indiafm.com/stills/celebrities/sada/thumb5.jpg'; 

$path="images/"; 


foreach($img as $i){ 
    save_image($i, $path); 
    if(getimagesize($path.basename($i))){ 
     echo '<h3 style="color: green;">Image ' . basename($i) . ' Downloaded OK</h3>'; 
    }else{ 
     echo '<h3 style="color: red;">Image ' . basename($i) . ' Download Failed</h3>'; 
    } 
} 

//Alternative Image Saving Using cURL seeing as allow_url_fopen is disabled - bummer 
function save_image($img,$fullpath='basename'){ 
    if($fullpath!='basename'){ 
     $fullpath = $fullpath.basename($img); 
    } 
    $ch = curl_init ($img); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); 
    $rawdata=curl_exec($ch); 
    curl_close ($ch); 
    if(file_exists($fullpath)){ 
     unlink($fullpath); 
    } 
    $fp = fopen($fullpath,'x'); 
    fwrite($fp, $rawdata); 
    fclose($fp); 
} 
0

正確工作添加

$agent= 'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11'; 

curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
+0

爲什麼要將「Accept」標頭設置爲用戶代理的一部分? – yakatz

0

我的日子不好過使用這種方法來訪問我的DLink的攝像頭。

但最後我發現問題:身份驗證。

不要忘記驗證。

這是爲我工作的解決方案,感謝所有貢獻者。

<?php 

function download_image1($image_url, $image_file){ 
    $fp = fopen ($image_file, 'w+');    // open file handle 

    $ch = curl_init($image_url); 

$agent= 'Accept:image/jpeg,text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11'; 

curl_setopt($ch, CURLOPT_USERAGENT, $agent); 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER,true); 
curl_setopt($ch, CURLOPT_TIMEOUT, 400); 
curl_setopt($ch, CURLOPT_AUTOREFERER, false); 
curl_setopt($ch, CURLOPT_REFERER, "http://google.com"); 
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); // Follows redirect responses. 
curl_setopt($ch, CURLOPT_USERPWD, "user:password"); 

$raw=curl_exec($ch); 
if ($raw === false) { 
    trigger_error(curl_error($ch)); 
} 

curl_close ($ch); 
$localName = $image_file; // The file name of the source can be used locally  
if(file_exists($localName)){ 
    unlink($localName); 
} 

$fp = fopen($localName,'x'); 
fwrite($fp, $raw); 
fclose($fp); 

} 

download_image1("http://url_here/image.jpg","/path/filename.jpg"); // to access DLink cameras 
// be sure you have rights to the path 
?> 

上面的代碼可能有一些冗餘,因爲我打開fopen兩次。說實話,我不會糾正,認爲它正在工作!