PHP采集图片的方法

包括一些防盗链的网站也能抓取到图片。 方法如下:

//$img1_path  存储地址(/User/uploads/a.jpg
//$img1 网络地址 (http://host/1.jpg)
function downloadImg($img1_path, $img1){
		//echo $img1_path ."==".$img1;
		if( $img1=='') return false;

		//echo $img1.'=='.$img1_path;
		//$file_content = @file_get_contents( $img1 );

		$curl = curl_init();
        //curl_setopt ($curl, CURLOPT_CUSTOMREQUEST, 'GET' );
        curl_setopt($curl, CURLOPT_HEADER, 0);//
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
		curl_setopt($curl, CURLOPT_TIMEOUT, 500);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_USERAGENT, "MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1");

		curl_setopt($curl, CURLOPT_URL, $img1 );
		$pars_arr = parse_url( $img1 );
		echo $pars_arr['scheme']."://".$pars_arr['host'].'====';
		curl_setopt ( $curl, CURLOPT_REFERER, $pars_arr['scheme']."://".$pars_arr['host'] );
       
		$contents = curl_exec($curl);

		curl_close($curl);

		if( $contents ){
			$fp = @fopen($img1_path, 'wb');
			@fwrite($fp,  $contents);
			@fclose($fp);
			return true;
		}
		return false;
	}

传相应的参数 即可采集下载图片到本地。