php curl下载图片
2023-05-06 本文已影响0人
耍帅oldboy
$url = 'https://www.xxx.com/165588089082943333.jpg';
$parse_url = parse_url($url);
function download($url, $path = './')
{
$heaers = [
'Host'=>'www.xxx.com',
':authority'=>'www.xxx.com'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $heaers);
$file = curl_exec($ch);
curl_close($ch);
$filename = pathinfo($url, PATHINFO_BASENAME);
file_put_contents($path.time().$filename,$file);
}
download($url);