Amazon S3通过PUBLIC URL下载图像文件

时间:2013-11-20 08:05:15

标签: php image url amazon-s3 amazon

我已在此位置使用Amazon S3服务上传了我的图片" https://s3.amazonaws.com/qbprod/" 上传后我得到回复,如

https://s3.amazonaws.com/qbprod/70dcdd564f5d4b15b32b975be15e4a1200

我尝试通过以下方式获取图像但未获得成功。

(1)................
    $xman = explode("/",$ImageSTR);
    //$saveto = end($xman).'.jpg';
    $saveto = end($xman);
    $fl = file_get_contents($ImageSTR);
    file_put_contents('abcd.jpg',$fl);

(2)................
    grab_image($ImageSTR,$saveto);  

    function grab_image($url,$saveto){
        $ch = curl_init ($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
        $raw=curl_exec($ch);
        curl_close ($ch);
        if(file_exists($saveto)){
            unlink($saveto);
        }
        $fp = fopen($saveto,'x');
        fwrite($fp, $raw);
        fclose($fp);
    }

先谢谢你

1 个答案:

答案 0 :(得分:3)

你可以看到这是https,你需要像这样使用CURLOPT_SSL_VERIFYPEER

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

这是最后的功能

function grab_image($url,$saveto){
    $ch = curl_init ($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $raw=curl_exec($ch);
    curl_close ($ch);
    if(file_exists($saveto)){
        unlink($saveto);
    }
    $fp = fopen($saveto,'x');
    fwrite($fp, $raw);
    fclose($fp);
}

您可以在此处阅读更多CURL http://php.net/manual/en/function.curl-setopt.php

希望这有帮助!