2

我制作了一个不错的简单用户脚本:
当我浏览网页时,我可以在 1 次单击
我的用户脚本 中为任何图像“添加书签”

  1. 获取img src
  2. 抓取网页的url
  3. 将 .jpg .png .gif 复制到我的服务器

一切正常,但在某些情况下,脚本无法复制文件...
实际上文件已创建但不包含 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); 

也许该网站阻止了那种“复制”脚本?谢谢!

4

4 回答 4

1

我能想到的2件事是,

  1. 为您的 curl 请求设置用户代理。因为从你所说的来看,你可以查看图像但是 curl 得到 403 错误,很可能是服务器端的 userAgent 过滤。

  2. 将引用者添加到您的 curl 请求。您可以将引用信息从您的用户脚本发送到 php 脚本。您必须发布或获取window.location.href' 值。

于 2012-09-05T17:45:22.213 回答
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);
于 2012-12-06T12:34:16.493 回答
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);
}
于 2012-09-05T19:42:18.587 回答
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 两次。老实说,我不会更正,因为它正在工作!

于 2015-12-27T13:30:40.810 回答