使用curl从SSL下载图像? [英] Download a image from SSL using curl?

查看:161
本文介绍了使用curl从SSL下载图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从curl(https站点)下载图像?



文件已保存在计算机中,为什么空白(0KB)?

  function save_image($ img,$ fullpath){
$ ch = curl_init($ img);
curl_setopt($ ch,CURLOPT_HEADER,1);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,0);
curl_setopt($ ch,CURLOPT_BINARYTRANSFER,1);
curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,0);
$ rawdata = curl_exec($ ch);
curl_close($ ch);

$ fp = fopen($ fullpath,’w’);
fwrite($ fp,$ rawdata);
fclose($ fp);
}

save_image( https://domain.com/file.jpg, image.jpg);


解决方案

  curl_setopt( $ ch,CURLOPT_RETURNTRANSFER,0); 

实际上应该是:

  curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1); 

所以curl知道它应该返回数据而不回显它。



此外,有时您需要做更多工作才能使curl接受SSL证书:

在PHP中使用cURL来访问受HTTPS(SSL / TLS)保护的网站



编辑:



鉴于您的用法,您还应按照Alix Axel的建议将CURLOPT_HEADER设置为false。 p>

关于SSL,我希望您能花些时间阅读我建议的链接,因为有几种处理SSL的方法,而Alix建议的修复方法可能是如果数据不敏感,但会否定安全性,则确定为OK,因为它强制CURL接受任何服务器证书。


How do I download a image from curl (https site)?

File is saved on my computer but why is it blank (0KB)?

function save_image($img,$fullpath){
    $ch = curl_init ($img);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0); 
    $rawdata=curl_exec($ch);
    curl_close ($ch);

    $fp = fopen($fullpath,'w');
    fwrite($fp, $rawdata);
    fclose($fp);
}

save_image("https://domain.com/file.jpg","image.jpg");

解决方案

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);

should actually be:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

so curl knows that it should return the data and not echo it out.

Additionally, sometimes you have to do some more work to get SSL certs accepted by curl:
Using cURL in PHP to access HTTPS (SSL/TLS) protected sites

EDIT:

Given your usage, you should also set CURLOPT_HEADER to false as Alix Axel recommended.

In terms of SSL, I hope you'll take time to read the link I suggested, as there are a few different ways to handle SSL, and the fix Alix recommended may be OK if the data isn't sensitive, but does negate the security, as it forces CURL to accept ANY SERVER CERTS.

这篇关于使用curl从SSL下载图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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