PHP实现并发下载


class BatchDown
{

	public $curl;
	public $task = array();
	
	function __construct(){
		$this->curl = curl_multi_init();
	}

	function __destruct(){
		curl_multi_close($this->curl);
	}

	function addTask($url, $file){
		$header = array(
			'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36',
			'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
			'Accept-Encoding: gzip, deflate',
			'Connection: keep-alive'
		);
		$c = curl_init($url);
		curl_setopt($c, CURLOPT_ENCODING, 'gzip');
		curl_setopt($c, CURLOPT_HTTPHEADER, $header);
		curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($c, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($c, CURLOPT_TIMEOUT, 10);
		curl_multi_add_handle($this->curl, $c);
		$this->task[] = array(
			'url' => $url,
			'curl' => $c,
			'file' => $file
		);
	}

	function run(){
		$running = null;
		do {
			curl_multi_exec($this->curl, $running);
			curl_multi_select($this->curl);
		} while($running > 0);
		foreach ($this->task as $v) {
			$data = curl_multi_getcontent($v['curl']);
			file_put_contents($v['file'], $data);
			curl_multi_remove_handle($this->curl, $v['curl']);
			curl_close($v['curl']);
		}
	}

}

$url = 'https://www.baidu.com/';
$task = new BatchDown();

foreach (range(1, 3) as $v) {
	$task->addTask($url, $v . '.html');
}

$task->run();