繁体   English   中英

如何从PHP中的动态网址获取/保存图像

[英]How can I fetch/save image from dynamic url in PHP

我如何从这样的动态网址获取/保存图片?

http://www.groove3.com/str/image.php?type=P&id=16470

我正在使用下面的功能,但不适用于动态网址:

function saveImage($image_url,$save_to){
    if(!file_exists($save_to) && $image_url != "")
    {
        $ch = curl_init($image_url);
        $fp = fopen($save_to, 'wb');
        curl_setopt($ch, CURLOPT_FILE, $fp);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_URL,$image_url);
        curl_exec($ch);
        curl_close($ch);
        fclose($fp); 
    }       

}

您可以改用file_get_contents

$img_content = file_get_contents($image_url);
//Save the file in file system.
$fp = fopen($save_to, "w");
fwrite($fp, $img_content);
fclose($fp);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM