doudunyi3796 2016-11-25 04:37
浏览 31

PHP Curl从VK保存动态图像

I am trying to save an image from VK to a folder. However the image does not save correctly.

The code I use is:

// imageFromURL (Save an image from a URL)
function imageFromURL($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);
}

imageFromURL('https://vk.com/captcha.php?sid=698254154192&s=1', 'Image/Captcha.png');

I think it must not be a standard image format? How could I save this and make sure it was a png format image with CURL?

  • 写回答

1条回答 默认 最新

  • duanjitong7226 2016-11-25 04:48
    关注

    You can use exif_imagetype($image) to return what type of image a file is. PNG would return 3.

    Additionally, you could check prior to saving:

    $mime_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
    

    and check for the mime type(s) you may be looking for. Mind you, there is some trust involved there because you're relying on the remote server to report that info correctly.

    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染