php中的(多线程)curl批量处理

function getMultiUrls($urls, $timeout = 30)
	{
		$queue = curl_multi_init();
		$map = array();
	
		foreach($urls as $id=>$url)
		{
			$ch = curl_init();
			curl_setopt($ch , CURLOPT_URL , $url);
			curl_setopt($ch , CURLOPT_TIMEOUT , $timeout);
			curl_setopt($ch , CURLOPT_RETURNTRANSFER , 1);
 			curl_setopt($ch, CURLOPT_HEADER, true);
// 			curl_setopt($ch , CURLOPT_HEADER , 0);
			curl_setopt($ch, CURLOPT_NOBODY, true);
			curl_setopt($ch , CURLOPT_NOSIGNAL , true);
	
			curl_multi_add_handle($queue , $ch);
			$map[(string) $ch] = $id;
		}
	
		$responses = array();
		do
		{
			while(($code = curl_multi_exec($queue , $active)) == CURLM_CALL_MULTI_PERFORM);
			if($code != CURLM_OK)
			{
				break;
			}
	
			// a request was just completed -- find out which one
			while($done = curl_multi_info_read($queue))
			{
	
				// get the info and content returned on the request
				$content = curl_multi_getcontent($done['handle']);
	
				$responses[$map[(string) $done['handle']]] = $this->parseHead($content);
	
				// remove the curl handle that just completed
				curl_multi_remove_handle($queue , $done['handle']);
				curl_close($done['handle']);
			}
	
			// Block for data in / output; error handling is done by
			// curl_multi_exec
			if($active > 0)
			{
				curl_multi_select($queue , 0.5);
			}
		}
		while($active);
		curl_multi_close($queue);
		return $responses;
	}
	
	function parseHead($headSream)
	{
		$headArrayTemp = explode("\r\n", $headSream);
		foreach ($headArrayTemp as $k=>$v)
		{
			if ($k==0)
			{
				$httpstas = explode(" ",$v);
				$headArray["http-edition"] = trim($httpstas[0]);
				$headArray["http-state"] = trim($httpstas[1]);
				$headArray["http-describe"] = "";
				for($i=2;$i<count($httpstas);$i++){
						$headArray["http-describe"] .= " ".trim($httpstas[$i]);
				}
			}
			else
			{
				if ($v == '')
				{
					break;
				}
				$headArray[strtolower(substr($v, 0, strpos($v, ':')))] = substr($v,strpos($v, ':')+1);
			}
		}
		
		return $headArray;
	}

One Reply to “php中的(多线程)curl批量处理”

发表评论

电子邮件地址不会被公开。 必填项已用*标注