如何使用PHP从URL保存图像? [英] How to save an image from url using PHP?

查看:118
本文介绍了如何使用PHP从URL保存图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

图片网址为: http://phim.xixam.com/thumb/giotdang.jpeg

我的代码是:

$img = 'http://phim.xixam.com/thumb/giotdang.jpeg';
file_puts_content('abc.jpg', file_get_contents($img));

但我收到警告:


file_get_contents( http://phim.xixam.com/thumb/giotdang.jpeg )[function.file-get-contents]:
无法打开流:HTTP请求失败! HTTP / 1.0 403禁止在
...

file_get_contents(http://phim.xixam.com/thumb/giotdang.jpeg) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in ...

我尝试通过 cURL 保存此图像但是也不行。

I try save this image by cURL but not work too.

推荐答案

php代码似乎在技术上是正确的但似乎由于某种原因你被阻止了。假设您是在同一台机器上的浏览器中尝试它并且它可以正常工作,我会猜测UserAgent字符串过滤。尝试使用带有cURL的有效用户代理字符串。

The php code seems to be technically correct but it seems that for one reason or another you are blocked. Assuming you are trying it from the browser on the same machine and it works, I would guess on an UserAgent string filtering. Try Using a valid user agent string with cURL.

来自 http://curl.haxx.se/docs/manpage.html


-A, - user-agent

-A, --user-agent

(HTTP)指定要发送到HTTP服务器的User-Agent字符串。如果此字段未设置为Mozilla / 4.0,则一些执行不良的CGI会失败。以
编码字符串中的空格,用单引号
标记包围字符串。当然也可以使用-H, - header选项进行设置。

(HTTP) Specify the User-Agent string to send to the HTTP server. Some badly done CGIs fail if this field isn't set to "Mozilla/4.0". To encode blanks in the string, surround the string with single quote marks. This can also be set with the -H, --header option of course.

如果多次设置此选项,则最后一个选项将是使用的选项。

If this option is set more than once, the last one will be the one that's used.

这篇关于如何使用PHP从URL保存图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆