php curl实现图片下载

$return_content = http_get_data($img_url);
        
file_put_contents($filename, $return_content);


function http_get_data($url) {    
        $ch = curl_init ();   
        curl_setopt($ch, CURLOPT_URL, $url );   
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_HTTPGET, true);
        curl_setopt($ch, CURLOPT_REFERER, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36');
        ob_start();    
        $ob_stream = curl_exec($ch);    
        echo $ob_stream;
        $return_content = ob_get_contents();    
        ob_end_clean();    
        curl_close($ch);

        return $return_content;    
}

 

你可能感兴趣的:(php)