CURL_MULTI 工具类

参考:http://techbbs.zol.com.cn/1/9_3371.html

<?php
/**
 * $results = CurlMulti::cmcheck ( array (
 * 		"http://www.maranellowebfashion.com/prodotti/immagini/scarpa_donna_elisabetta_franchi_sa7962052_rosa_212/1.jpg",
 * 		"http://www.maranellowebfashion.com/prodotti/immagini/scarpa_donna_elisabetta_franchi_sa7962052_rosa_212/222.jpg",
 * 		"http://www.maranellowebfashion.com/prodotti/immagini/scarpa_donna_elisabetta_franchi_sa7962052_rosa_212/3.jpg" 
 * 	) );
 * $results = CurlMulti::cmfetch($results);
 * @author caihaibin
 */
class CurlMulti {
	static $urls = null;
	static $able_urls=null;
	static $resourcs = null;
	static $resoponses = null;
	static $nobody = false;
	static $mh = null;
	static function cminit($urls) {
		self::$resourcs = array ();
		self::$resoponses = array ();
		self::$urls = $urls;
		self::$mh = curl_multi_init ();
		foreach ( $urls as $url ) {
			$ch = curl_init ();
			curl_setopt ( $ch, CURLOPT_URL, $url );
			curl_setopt ( $ch, CURLOPT_HEADER, 0 );
			// 执行不打印
			curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); // return the image
			if (self::$nobody)
				curl_setopt ( $ch, CURLOPT_NOBODY, 1 );
			curl_multi_add_handle ( self::$mh, $ch );
			self::$resourcs [] = $ch;
		}
	}
	static function cmcheck($urls = array()) {
		self::$nobody = true;
		self::$able_urls = array();
		self::cminit ( $urls );
		$active = null;
		// 执行批处理句柄
		do {
			$mrc = curl_multi_exec ( self::$mh, $active );
		} while ( $mrc == CURLM_CALL_MULTI_PERFORM );

		while ( $active && $mrc == CURLM_OK ) {
			if (curl_multi_select ( self::$mh ) != - 1) {
				do {
					$mrc = curl_multi_exec ( self::$mh, $active );
				} while ( $mrc == CURLM_CALL_MULTI_PERFORM );

				if ($mhinfo = curl_multi_info_read ( self::$mh )) {
					// 意味着该连接正常结束
					// 8. 从curl句柄获取信息
					$chinfo = curl_getinfo ( $mhinfo ['handle'] );
					// 9. 死链么?
					if (! $chinfo ['http_code']) {
// 						$chinfo ['url'];
						// 10. 404了?
					} else if ($chinfo ['http_code'] == 404) {
// 						$chinfo ['url'];
						// 11. 还能用
					} else {
						$able_urls[]=$chinfo ['url'];
					}
					// 12. 移除句柄
					curl_multi_remove_handle ( self::$mh, $mhinfo ['handle'] );
					curl_close ( $mhinfo ['handle'] );
					// 13. 加入新URL,干活
					do {
						$mrc = curl_multi_exec ( self::$mh, $active );
					} while ( $mrc == CURLM_CALL_MULTI_PERFORM );
				}
			}
		}
		curl_multi_close ( self::$mh );
		return $able_urls;
	}
	static function cmfetch($urls = array()) {
		self::$nobody = false;
		self::cminit ( $urls );
		$active = null;
		// 执行批处理句柄
		do {
			$mrc = curl_multi_exec ( self::$mh, $active );
		} while ( $mrc == CURLM_CALL_MULTI_PERFORM );

		while ( $active && $mrc == CURLM_OK ) {

			if (curl_multi_select ( self::$mh ) != - 1) {

				do {
					$mrc = curl_multi_exec ( self::$mh, $active );
				} while ( $mrc == CURLM_CALL_MULTI_PERFORM );

			}

		}
		self::cmdestroy ();
		return self::$resoponses;
	}
	static function getContents() {
		return self::$resoponses;
	}
	static function cmdestroy() {
		foreach ( self::$resourcs as &$rs ) {
			self::$resoponses [] = curl_multi_getcontent ( $rs );
			curl_multi_remove_handle ( self::$mh, $rs );
		}
		curl_multi_close ( self::$mh );
	}
}

转发请注明出处http://blog.martoo.cn
如有漏缺,请联系我 QQ 243008827

发表评论

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