从没有扩展名的URL获取图像时使用PHP CURL时出错

时间:2018-07-29 04:16:00

标签: php curl php-curl

function get_image($url) {
    $saveto = '/image/'.basename($url);

    $ch = curl_init ($url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $raw = curl_exec($ch);
    curl_close ($ch);
    if(file_exists($saveto)){
        unlink($saveto);
    }
    $fp = fopen($saveto, 'w');
    fwrite($fp, $raw);
    fclose($fp);

    return basename($url);
}

这是网址:https://cdn-images.kiotviet.vn/mirdecor/93029d186a5a416796cb9910a7583a1d

结果是:93029d186a5a416796cb9910a7583a1d,格式错误的图片(jpg,png,jpeg等)

0 个答案:

没有答案