图像由php curl或file_get_contents下载

时间:2013-12-13 09:48:21

标签: php curl file-get-contents imagedownload

我正在尝试从here

下载图片

以上网址使用php curl和file_get_content。但是当我使用php curl时,图像被下载但没有内容,当我使用file_get_content时会发生以下错误,

Warning: file_get_contents(http://s3.amazonaws.com/test_bucket/test_12_12_2013_10_51_00.jpg) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 505 HTTP Version Not Supported in /home/webdev/public_html/example/photos/download.php on line 11

1 个答案:

答案 0 :(得分:1)

首先,您提供的链接不是图片。它输出..

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Error>
<Code>AllAccessDisabled</Code>
<Message>All access to this object has been disabled</Message>
<RequestId>35DFBD44D75B45C4</RequestId>
<HostId>
n1TT+gOUuMJ3RydGND/s3QL73JJEadz/2uQutK4NNUDbEaGX6EI0Y8a/6eDHLR6H
</HostId>
</Error>

经测试&amp;工作!

下面的代码从URL中读取图像,保存,然后在浏览器上打开/渲染。

<?php
$image ="http://blog.stackoverflow.com/wp-content/uploads/stackoverflow-logo-300.png";
$ch = curl_init($image);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec ($ch);
curl_close ($ch);
$fp = fopen("test.jpg",'w');
fwrite($fp, $rawdata);
fclose($fp);
header ("Content-Type: image/png");
readfile("test.jpg");

输出:

enter image description here

如果你想要渲染它..就像这样。

<?php
readfile("http://blog.stackoverflow.com/wp-content/uploads/stackoverflow-logo-300.png");
header ("Content-Type: image/png");