下载图片网址 - PHP [英] Download Image URL - PHP

查看:130
本文介绍了下载图片网址 - PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个脚本将图像下载到本地服务器,我在下载图像时遇到一些问题。我已经在标准图片上测试了以下脚本( http://www.urmob。当尝试从以下位置下载图片时,它的工作正常。

I'm trying to create a script to download images to a local server and I'm having some issues with the images downloading. I have tested the following script on a standard image (http://www.urmob.co.uk/i/l/2860.jpg) and its working fine, however, when trying to download an image from the following location

https ://s3-eu-west-1.amazonaws.com/mobile-phones-direct/phone/portraitimage/full/52122ad8-c918-450d-be10-5921c0a870cb.png

它没有文件内容,因此图像不工作。我相信它与HTTPS有关系?我最初尝试标准的file_get_contents但是没有工作,现在我正在尝试一个CURL版本,它仍然不工作。我使用的功能如下所示:

It has no file contents and therefore the image doesn't work. I believe it has something to do with the HTTPS? I originally tried the standard file_get_contents but that didn't work, now I'm trying a CURL version and it is still not working. The function I'm using looks like the below:

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);
}

有人知道为什么这不工作吗?

Does anyone know why this isn't working?

感谢,

Craig

推荐答案

这很简单:

test.php

<?php

// image source
$image_url = 'https://s3-eu-west-1.amazonaws.com/mobile-phones-direct/phone/portraitimage/full/52122ad8-c918-450d-be10-5921c0a870cb.png';

// download image
file_put_contents('downloaded_image.jpg', file_get_contents($image_url));

// display
echo '<img src="downloaded_image.jpg" >';

它适用于我:

img src =https://i.stack.imgur.com/8zOae.pngalt =输入图片说明here>>

如果你想继续使用curl版本并修复ssl错误,请将它添加到你的代码:

If you want to keep using the curl version and fix that ssl error, add this to your code:

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

这篇关于下载图片网址 - PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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