【问题标题】:image download by php curl or file_get_content通过 php curl 或 file_get_content 下载图片
【发布时间】:2014-01-01 00:55:53
【问题描述】:

我正在尝试从here下载图片

同时使用 php curl 和 file_get_content 获取上述 url。但是当我使用 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

【问题讨论】:

    标签: php curl file-get-contents imagedownload


    【解决方案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>
    

    测试和工作!

    以下代码从 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");
    

    输出:

    或者

    如果你只想渲染它.. 这样做。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-09
      • 1970-01-01
      • 1970-01-01
      • 2011-02-10
      • 2020-10-29
      • 1970-01-01
      • 1970-01-01
      • 2013-11-27
      相关资源
      最近更新 更多