【问题标题】:How can I fetch/save image from dynamic url in PHP如何从 PHP 中的动态 url 获取/保存图像
【发布时间】:2015-07-07 22:29:24
【问题描述】:

我怎样才能像这样从动态 url 获取/保存图像?

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); 
    }       

}

【问题讨论】:

    标签: php curl web-scraping


    【解决方案1】:

    您可以改用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);
    

    【讨论】:

      猜你喜欢
      • 2012-12-20
      • 2010-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-16
      • 2018-03-23
      • 2010-12-20
      相关资源
      最近更新 更多