Heim >Backend-Entwicklung >PHP-Tutorial >有一个图片的下载链接,php如何下载图片到本地
比如图片链接地址如下:http://125.35.6.80:8080/ftba/itownet/download.do?method=downloadFile&fid=917d9f52486042a4829ea9392b801f6d&ssid=KV_L24clMSHsqY7punwrgA||
使用PHP如何自动的下载到本地,我使用file_get_contents和curl两种方法均未成功。
我使用的方法代码如下:
1)file_get_contents:
$img = file_get_contents(‘http://125.35.6.80:8080/ftba/itownet/download.do?method=downloadFile&fid=917d9f52486042a4829ea9392b801f6d&ssid=KV_L24clMSHsqY7punwrgA||’);
file_put_contents('1.jpg',$img);
2)curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch,CURLOPT_URL,‘http://125.35.6.80:8080/ftba/itownet/download.do?method=downloadFile&fid=917d9f52486042a4829ea9392b801f6d&ssid=KV_L24clMSHsqY7punwrgA||’);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$file_content = curl_exec($ch);
curl_close($ch);
$downloaded_file = fopen('1.jpg', 'w');
fwrite($downloaded_file, $file_content);
fclose($downloaded_file);
望指教,在此谢过。
curl 缺少:
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
file_get_contents 缺少第三个参数数:
$context = stream_context_create(array('http' => array('user_agent' => $_SERVER['HTTP_USER_AGENT'])));
http://blog.csdn.net/shrimpma/article/details/9816035