下载受热链接保护的图像



我想下载一个受热链接保护的图像。如何使用CURL伪造HTTP标头,以表示referer来自其自己的服务器?

我试过这个命令,但失败了。我不熟悉PHP,如果有帮助的话,我会非常感激。

curl -A "Mozilla/5.0" -L -b /tmp/c -c /tmp/c -s 'http://remote-site.com/image.jpg' > image.jpg

选项看起来是带有curl_setoptCURLOPT_REFERERcurl --referer,但不确定正确的语法。


编辑2:

我得到一个错误,说curl_setopt()期望参数2很长。删除静音选项后,错误消失。

为了显示图像,我尝试了这个代码,但页面仍然是空白的。

$image = curl_exec($ch);
curl_close($ch);
fclose($fp);
print '<img src="'.$image.'"/>';

编辑1:

我在Wordpress帖子中输入的代码(我使用插件Insert PHP)

[insert_php]
curl --referer http://www.DOMAIN.com/ -A "Mozilla/5.0" -L -b /tmp -c /tmp -s 'http://www.DOMAIN.com/image.png' > image.png
[/insert_php]

当我加载页面时出现的错误:

Parse error: syntax error, unexpected ‘<‘ in /public_html/wp-content/plugins/insert-php/insert_php.php(48) : eval()’d code on line 8

您应该能够指定referer作为curl的选项,如下所示:

curl --referer http://remote-site.com/ -A "Mozilla/5.0" -L -b /tmp/c -c /tmp/c -s 'http://remote-site.com/image.jpg' > image.jpg

curl的语法很简单:

curl [options...] <url>

刚刚注意到:由于您已经用-s指定了静默模式,所以应该用--output <file>参数指定输出文件。使用-s选项时,您不能使用输出重定向(> image.jpg),因为一开始没有输出。

更新:

您必须在[insert_php][/insert_php]标记之间插入PHP代码。您现在拥有的字符串不是有效的PHP代码。您必须使用PHP提供的curl_*函数。你的代码应该是这样的:

$ch = curl_init();
$fp = fopen("image.jpg", "w");
curl_setopt($ch, CURLOPT_URL, "http://remote-site.com/image.jpg");
curl_setopt($ch, CURLOPT_MUTE, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/c");
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/c");
curl_setopt($ch, CURLOPT_REFERER, "http://remote-site.com/");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0");
curl_exec($ch);
curl_close($ch);
fclose($fp);

最新更新