php-curlmulti

作者: admin 分类: 未分类 发布时间: 2015-11-29 01:15

https://github.com/ares333/php-curlmulti
每次100个任务
简单的例子:

<?php
	set_time_limit(0); 
	header("Content-type: text/html; charset=UTF-8");
	include 'Core.php';
	include 'My.php';
	class Demo extends CurlMulti_My {
		private $url = "http://localhost/";
		private $ua = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)";
		private $noCacheNum = 10;
		private $imgDir;
		private $page = 1;
		//private $page = 1;
		function __construct($curl ) {

			parent::__construct ( $curl );
			$cacheDir = __DIR__ . '/cache';
			$this->imgDir = __DIR__ . '/image';
			if (! is_dir ( $cacheDir ))
				mkdir ( $cacheDir );
			if (! is_dir ( $this->imgDir ))
				mkdir ( $this->imgDir );
			//$this->getCurl ()->cache ['dir'] = $cacheDir;
			//$this->getCurl ()->cache ['enalbe'] = true;
			$this->getCurl ()->maxThread = 80;
			$this->getCurl ()->opt [CURLOPT_CONNECTTIMEOUT] = 10;
			$this->getCurl ()->cbInfo = array (
				$this,
				'cbCurlInfo' 
			);
			$this->getCurl ()->maxThreadType ['img'] = 30;
		}
		function myStrat(){
			//first 10 task
			for(;$this->page < 10;$this->page++){
				$this->getCurl ()->add(
					array (
						'url' => $this->url."?page=".$this->page,
						'args' => array (
							'page' => $this->page, 
						),
						'opt' => array (
						) 
					), 
					array (
						$this,
						'cb1' 
					) 
				);
			}
			$this->getCurl ()->start();
		}
		//if success
		function cb1($r, $param){
			//next page
			//echo $r["content"];
			
			if($this->page > 100){
				return 0;
			}else{
				if (! $this->hasHttpError ( $r ['info'] )){
					echo $this->page;
					echo "\n";
					$this->page = $this->page+1;
					//add get next page list task
					$this->getCurl ()->add(
						array (
							'url' => $this->url."?page=".$this->page,
							'args' => array (
								'page' => $this->page, 
							),
							'opt' => array (

							) 
						), 
						array (
							$this,
							'cb1',
						) 
					);
					
				}
			}
			
			
		}
	}
	$demo = new Demo ( new CurlMulti_Core () );
	$demo->myStrat ();
	
	
?>

发表评论

标签云