dongwen5019 2014-03-11 10:45
浏览 84

使用PHP中的cURL从Facebook下载时图像被破坏

Here's the script I use to download an image from Facebook

function downloadImage($image_url)
{
  // Set filename
  $filename = dirname(__FILE__).'/wow.jpg';
  // Proceed to download
  // Open file to save
  $file = fopen($filename, 'w');
  // Use curl
  $ch = curl_init($image_url);
  // Set options
  curl_setopt($ch, CURLOPT_FILE, $file);
  curl_setopt($ch, CURLOPT_ENCODING, ''); 
  curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0'); 
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); 
  // Execute
  $data = curl_exec($ch);
  // Close curl
  curl_close($ch);
  // Close file
  fclose($file);
}

// Download image
downloadImage('https://graph.facebook.com/WowSuchPage/picture?width=720&height=720');

The download succeeds though, but when I open the image file, it appears to be broken. Here's the image that gets downloaded:

Downloaded Image

This only occurs when the image source is from Facebook, any other domains are OK. I don't think it has something to do with my ISP though because if I download the image through my browser, it's all fine

I hope you can help me on this one as this has been bugging me for some time now. Thanks!

EDIT

By the way, I'm using Wampserver 2.4 on localhost. My PHP version is 5.4.12

FIXED

Alright, I finally found the issue. It seems either cURL or the SSL component/extension in my local wampserver is the root of the issue. I only had to use "http://" rather than "https://" to access the image perfectly

But it would really be great if I can still download the image as perfect as it should be even in https://, I won't close this one yet until I find some answers. Thanks for your help

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
    • ¥20 Python安装cvxpy库出问题
    • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
    • ¥15 python天天向上类似问题,但没有清零
    • ¥30 3天&7天&&15天&销量如何统计同一行
    • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
    • ¥15 C#调用python代码(python带有库)
    • ¥15 活动选择题。最多可以参加几个项目?
    • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
    • ¥15 vs2019中数据导出问题