php爬取图片
重点是要设置 user_agent , 不然有会报错
http failed to open stream
ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;)');
function putImgToLocal($url) {
//创建目录
if (!is_dir('./sharePic')) {
mkdir('./sharePic', 0777, true);
}
$file_name = substr($url,strrpos($url,"/") + 1);
$imgBin = file_get_contents($url);
file_put_contents('./sharePic/'.$file_name, $imgBin);
}