简体   繁体   中英

How to save an image from url using PHP?

The image URL is: http://phim.xixam.com/thumb/giotdang.jpeg

My code is:

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

But I receive Warning:

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 ...

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

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.

From http://curl.haxx.se/docs/manpage.html :

-A, --user-agent

(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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM