【问题标题】:PHP retrieve and save remote imagePHP检索并保存远程图像
【发布时间】:2011-09-10 22:10:49
【问题描述】:

通过以下在 PHP 中检索文件的方法,我不断点击反盗链安全图片,而不是我正在寻找的图片。奇怪的是,当我在 Firefox/IE 甚至 Internet 下载管理器中手动输入 url 时,我确实得到了正确的文件,所以我到目前为止尝试的方法一定有问题。

file_put_contents:

file_put_contents($localpath, file_get_contents($remoteURL));

以下功能也不起作用:

function save_image($inPath,$outPath)
{ //Download images from remote server
    $in=    fopen($inPath, "rb");
    $out=   fopen($outPath, "wb");
    while ($chunk = fread($in,8192))
    {
        fwrite($out, $chunk, 8192);
    }
    fclose($in);
    fclose($out);
}
save_image($remoteURL,$localpath);

和 fopen()

$tag = fopen($remoteURL, 'rb');

if($tag){
 while(!feof($tag)) {
     $imgt = $imgt . fread($tag, 1024);
 }
} 

而 imagecreatefromjpeg() 也没有做到这一点

function LoadJpeg($imgname)
{
    /* Attempt to open */
    $im = @imagecreatefromjpeg($imgname);

    /* See if it failed */
    if(!$im)
    {
        /* Create a black image */
        $im  = imagecreatetruecolor(150, 30);
        $bgc = imagecolorallocate($im, 255, 255, 255);
        $tc  = imagecolorallocate($im, 0, 0, 0);

        imagefilledrectangle($im, 0, 0, 150, 30, $bgc);

        /* Output an error message */
        imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
    }

    return $im;
}

我还有其他选择吗?

【问题讨论】:

  • 问题可能是您发送的 HTTP Referer 标头的值。该网站故意试图阻止你做你想做的事——在不先浏览他们的网页的情况下获取图像。您也可能违反了他们的条款和条件 - 您应该在继续之前阅读这些条款和条件。
  • 你的php.ini系统中allow_url_fopen的值是多少?
  • @atma 开着了,不可能是这样

标签: php


【解决方案1】:

如果这不是您的网站,那么您几乎无能为力。该网站的设立是为了阻止人们删除他们的图片或在他们的链接中使用它们。

但是,您可以使用 curl 语句而不是 file_get_contents,如果这不起作用,请使用 curl 发送一些带有请求的浏览器标头。

如果这是您的网站,如果您在 cPanel 上运行,则安全部分有一个选项。我忘了具体在哪里。

【讨论】:

    【解决方案2】:

    既然你写了 allow_url_fopenon 并且函数不起作用,你可以试试 curl。

    function save_image($inPath,$outPath){
        $ch = curl_init ($inPath);
        curl_setopt($ch, CURLOPT_HEADER, 0); // required
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); // required for images
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // maybe redirect on other side?
        curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3'); // or user agent checks?
        $rawdata=curl_exec($ch);
        curl_close ($ch);
        if(file_exists($outPath)){
            @unlink($outPath);
        }
        $fp = fopen($outPath,'x');
        fwrite($fp, $rawdata);
        fclose($fp);
    }
    

    【讨论】:

    • 这真是太奇怪了.. 仍然得到 404 替换图像。同样,如果我复制链接并将其粘贴到 Firefox 中(甚至完全在另一台机器上),我只会得到正常的图像。我们会丢失什么?
    • 原链接:) 可以给吗?
    • 恐怕不行,内容可能与stackoverflow规则冲突。我只是觉得这很奇怪,因为我想我所要做的就是输入 URL->保存图像,就像我手动完成一样,但看起来这正是我们正在“模拟”的。
    • 但是来自 wget 的消息是什么?你登录过这个网站吗?该网站需要登录吗?
    • 无需登录,我只是将直接链接发送给在线朋友,他可以很好地获取图像,这意味着唯一出错的是提供图像的 php 脚本以某种方式检测到它是脚本保存文件而不是人类。 Wget 只是说“已发送 HTTP 请求,响应 200 OK”,然后它会下载 403.gif(不是 404,我的错)而不是实际图像。
    【解决方案3】:

    他们那边可能有一些检查,他们可能正在获取用户代理,推荐人。尝试将此用于fake 浏览器的行为

    【讨论】:

    • 我假设我需要改变它; curl_setopt($ch, CURLOPT_HEADER, 0);它是否正确?我也在查看 CURLOPT_HTTPHEADER 但我无法从您提供的链接中真正弄清楚如何处理它。
    • @natli:没有。 CURLOPT_USERAGENT 和 CURLOPT_REFERER
    • 即使在 ;X 上使用 useragent 和 referer 时仍然得到 404
    • @natli:奇怪。可能会有更多的安全检查。
    猜你喜欢
    • 1970-01-01
    • 2018-07-07
    • 2011-12-20
    • 2012-05-16
    • 2021-04-24
    • 2015-08-22
    • 2023-03-21
    • 1970-01-01
    • 2022-01-13
    相关资源
    最近更新 更多