无法将图像从 Tumblr 复制到我的网站,连接超时错误

Can't copy images from Tumblr to my site, connection timed out error

Tumblr 图片示例:

https://69.media.tumblr.com/ec3874e541a767ab578495b762201a53/tumblr_ph9je0Akck1r89eh5o1_1280.jpg

代码:

<form method="get">
    <input type="text" name="url"/>
    <input type="submit" value=" загрузить "/>
</form>

<?php
 $name = md5(date('Y-m-d H:i:s').rand(0, 1000));
 $folder = 'upload/';
 $source = ($_GET['url']);
 $dest = $folder.$name.'.png';

 copy($source, $dest);
 echo 'http://mysite.example/'.$folder.$name.'.png';
?>

我在本网站的另一个问题中发现了这一点:

If you can view the image in a browser that is on the same machine as your program, then it might be that the server won't send the picture unless you look like a user rather than a program. In that case, modifying the browser identification string might fix your problem. If you cannot view the image from a browser running on the program's PC, you will need to look elsewhere for the source of your problem.

我想,我遇到的问题与此类似。 Tumblr 提供图片以在浏览器中查看,但不允许使用脚本复制它。

如何解决?例如,像 imgur 这样的网站可以通过 url 毫无问题地上传 Tumblr 图片。

P.S。对于来自其他站点的图像,使用此脚本复制正常。

加法 01:

事实证明,问题出在我的网站上。当我 运行 此代码在另一个站点上时,它可以正常使用 Tumblr 图片。我有一个免费域名 .ml 和免费托管 Byethost。我有两个猜测。首先,我的域或托管在 Tubmlr 的黑名单中。第二个,我的网站上有一些错误的设置。如果第一个猜测是正确的,有没有办法让它在不改变域或主机的情况下工作?如果第二个为真,我必须检查和更改哪些设置?

Tumblr 似乎正在检查 HTTP 请求并根据您的获取方式生成不同的响应。如您所知,您的代码适用于大多数网站。当我 运行 它 as-is 时,我收到 403 拒绝错误。

更改代码以改用 Curl 允许我下载您的文件。我的猜测是 PHP 的 copy() 中使用的默认 headers 被阻止了。

<?php
function grab_image($url,$saveto){
    $ch = curl_init ($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    $raw=curl_exec($ch);
    curl_close ($ch);
    if(file_exists($saveto)){
        unlink($saveto);
    }
    $fp = fopen($saveto,'x');
    fwrite($fp, $raw);
    fclose($fp);
}

$name = md5(date('Y-m-d H:i:s').rand(0, 1000));
$folder = 'upload/';
$source = ($_GET['url']);
$dest = $folder.$name.'.png';

grab_image($source, $dest);

grab_image() 函数是另一个问题的 SO answer