基于curl的php多线程类(异步请求)

来源:http://blog.csdn.net/sunjier/article/details/8375073

  1. <?php  
  2. /** 
  3.  * @name CurlRequest php 请求类 
  4.  * @license 基于curl 实现, 可模拟多线程任务 
  5.  */  
  6. class CurlRequest  
  7. {  
  8.     /** 
  9.      * @name 成员变量 
  10.      */  
  11.     // param  
  12.     protected $url;         // url参数  
  13.     protected $data;        // data参数  
  14.     // request  
  15.     protected $request_url              = '';       // 请求地址  
  16.     protected $request_data             = array();  // 请求参数  
  17.     protected $request_timeout          = 30;       // 请求超时时间(单位秒)  0为无限等待  
  18.   
  19.     /** 
  20.      * @name 请求地址 
  21.      * @param $url 
  22.      */  
  23.     public function url($url)  
  24.     {  
  25.         $this->url   = $url;  
  26.   
  27.         $parseUrl   = parse_url($url);    
  28.         $this->request_url   = '';  
  29.         $this->request_url   .= $parseUrl['scheme']=='https' ? 'https://' : 'http://';  
  30.         $this->request_url   .= $parseUrl['host'];  
  31.         $this->request_url   .= $parseUrl['port'] ? ':'.$parseUrl['port'] : ':80';  
  32.         $this->request_url   .= $parseUrl['path'];  
  33.         parse_str($parseUrl['query'], $parseStr);  
  34.         $this->request_data  = array_merge($this->request_data, $parseStr);  
  35.   
  36.         return $this;  
  37.     }  
  38.   
  39.     /** 
  40.      * @name 请求数据 
  41.      * @param $data 为数组 
  42.      */  
  43.     public function data($data)  
  44.     {  
  45.         $this->request_data = array_merge($this->request_data, $data);  
  46.         return $this;  
  47.     }  
  48.   
  49.     /** 
  50.      * @name 请求超时时间 
  51.      * @param $timeout 超时, 当timeout 为0 或 flase时, 类为多线程执行 
  52.      */  
  53.     public function timeout($timeout)  
  54.     {  
  55.         // $this->request_timeout    = (int)$timeout==0 ? 1 : (int)$timeout;  
  56.         $this->request_timeout   = (int)$timeout;  
  57.         return $this;  
  58.     }  
  59.   
  60.     /** 
  61.      * @name get请求 
  62.      * @return mixed [status, data] 
  63.      */  
  64.     public function get()  
  65.     {  
  66.         $returnData;  
  67.         // 1. 初始化  
  68.         $ch = curl_init();  
  69.         // 2. 设置选项,包括URL  
  70.         $url = $this->request_url.'?'.http_build_query($this->request_data);  
  71.         curl_setopt($ch, CURLOPT_HTTPGET, 1);           // 请求类型 get  
  72.         curl_setopt($ch, CURLOPT_URL, $url);            // 请求地址  
  73.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    // 将curl_exec()获取的信息以文件流的形式返回,不直接输出。  
  74.         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->request_timeout);    // 连接等待时间  
  75.         curl_setopt($ch, CURLOPT_TIMEOUT, $this->request_timeout);           // curl允许执行时间  
  76.   
  77.         // 3. 执行并获取返回内容  
  78.         $output = curl_exec($ch);  
  79.         if ($output === false)  
  80.         {  
  81.             $returnData['status']   = 0;  
  82.             $returnData['data']     = curl_error($ch);  
  83.         }  
  84.         else  
  85.         {  
  86.             $returnData['status']   = 1;  
  87.             $returnData['data']     = $output;            
  88.         }  
  89.         // 4. 释放curl句柄  
  90.         curl_close($ch);  
  91.         return $returnData;  
  92.     }  
  93.     /** 
  94.      * @name post请求 
  95.      * @return mixed [status, data] 
  96.      */  
  97.     public function post()  
  98.     {  
  99.         $returnData;  
  100.         // 1. 初始化  
  101.         $ch = curl_init();  
  102.         // 2. 设置选项,包括URL  
  103.         curl_setopt($ch, CURLOPT_POST, 1);                  // 请求类型 post  
  104.         curl_setopt($ch, CURLOPT_URL, $this->request_url);   // 请求地址  
  105.         curl_setopt($ch, CURLOPT_POSTFIELDS, $this->request_data);   // 请求数据       
  106.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        // 将curl_exec()获取的信息以文件流的形式返回,不直接输出。  
  107.         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->request_timeout);    // 连接等待时间  
  108.         curl_setopt($ch, CURLOPT_TIMEOUT, $this->request_timeout);           // curl允许执行时间  
  109.         // 3. 执行并获取返回内容  
  110.         $output = curl_exec($ch);  
  111.         if ($output === false)  
  112.         {  
  113.             $returnData['status']   = 0;  
  114.             $returnData['data']     = curl_error($ch);  
  115.         }  
  116.         else  
  117.         {  
  118.             $returnData['status']   = 1;  
  119.             $returnData['data']     = $output;            
  120.         }  
  121.         // 4. 释放curl句柄  
  122.         curl_close($ch);  
  123.         return $returnData;  
  124.     }  
  125. }  

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值