使用cURL脚本复制img时出错(php)

我做了一个很好的简单的userscript:
当我浏览网页时,我可以通过单击“书签”任何图像
我的用户

  • 抓住img src
  • 抓取网页的网址
  • 将.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); 
    

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


    我能想到的两件事情是,

  • 将用户代理设置为卷曲请求。 因为根据你的说法,你可以查看图像,但curl得到了403错误,很可能是服务器端的userAgent过滤。

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


  • 尝试下面的代码,它在我的服务器上正常工作。 它是测试代码: -

    <?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);
    }
    

    为正确的工作添加

    $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);
    
    链接地址: http://www.djcxy.com/p/69637.html

    上一篇: Error when copying img with a cURL script (php)

    下一篇: Organization of files in Code Blocks