Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15080
  • 博文数量: 11
  • 博客积分: 130
  • 博客等级: 入伍新兵
  • 技术积分: 130
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-07 13:21
文章分类

全部博文(11)

文章存档

2015年(1)

2014年(5)

2011年(5)

我的朋友
最近访客

分类: Python/Ruby

2011-04-13 20:26:29

  1. class CURLS {
  2.     var $url;
  3.     var $source;
  4.     var $content;
  5.     public function __construct($url,$fromdata=array(),$timeout=false){
  6.         $this->url=$url;
  7.         $this->source=curl_init();
  8.         $this->setopt(CURLOPT_URL, $this->url);
  9.         if(is_array($formdata)&&$formdata){
  10.             $formdata = http_build_query($data);
  11.             $this->setopt(CURLOPT_POST, true);
  12.             $this->setopt(CURLOPT_POSTFIELDS, $formdata);
  13.             
  14.         }
  15.         if($timeout>0){
  16.             $this->setopt(CURLOPT_CONNECTTIMEOUT,$timeout);
  17.             $this->setopt(CURLOPT_TIMEOUT,$timeout);
  18.         }

  19.     }
  20.     
  21.     public function __destruct(){
  22.         curl_close($this->source);
  23.     }
  24.     
  25.     public function setopt($option,$value){
  26.         curl_setopt($this->source,$option,$value);
  27.     }
  28.     
  29.     /**
  30.      * 一个快速的curl配置
  31.      * @return
  32.      */
  33.     public function qkset(){
  34.         $this->setopt(CURLOPT_RETURNTRANSFER, 1);
  35.         $this->setopt(CURLOPT_HEADER, 0);
  36.     }
  37.     
  38.     public function getContent(){
  39.         curl_exec($this->source);
  40.         $this->content=curl_multi_getcontent ($this->source);
  41.         return $this->content?$this->content:FALSE;
  42.     }
  43. }

  44. class CurlFactory{
  45.     var $curl_array=array();
  46.     var $master;
  47.     var $results=array();
  48.     var $num=0;
  49.     public function __construct($urls,$formdatas=array()){
  50.         if(is_array($urls)&&count($urls)>=count($formdata)){
  51.             $this->num=count($urls);
  52.             $this->master=curl_multi_init();
  53.             foreach($urls as $k=>$url){
  54.                 $this->curl_array[$k]=new CURLS($url,$formdatas[k]);
  55.                 $this->curl_array[$k]->qkset();
  56.                 $this->addHandle($this->curl_array[$k]->source);
  57.             }
  58.         }
  59.         
  60.     }
  61.     
  62.     public function addHandle($source){
  63.         curl_multi_add_handle($this->master,$source);
  64.     }
  65.     
  66.     public function run(){
  67.         do{
  68.             curl_multi_exec($this->master, $running);
  69.         }while($running>0);
  70.         
  71.         foreach($this->curl_array as $k =>$v){
  72.             $this->results[$k]=curl_multi_getcontent($v->source);
  73.         }
  74.     }
  75.     
  76.     public function getContent($i=false){
  77.         if(is_int($i)&&$i<$this->num){
  78.             return $this->results[$i];
  79.         }
  80.         return $this->results?$this->results:FALSE;
  81.     }
  82. }





  1. $url="http://blog.chinaunix.net/space.php?uid=23708392";
  2.  $curl=new CURLS($url,FALSE,100);
  3.  $curl->qkset();
  4.  var_dump($curl->getContent());

  1. // $urls=array('','','','');
  2. // $cf=new CurlFactory($urls,array(array('name'=>'js')));
  3. // $cf->run();
  4. // var_dump($cf->getContent());
阅读(457) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~