1 <?php
2
3 /* curl多线程打开类
4 * curl_multi 2012年3月15日 21:39:32做了一小修改..在非200 301 302的情况下返回空!
5 * by wc1217
6 */
7
8 class curl_multi{
9
10 //Curl句柄
11 //private $curl_handle = null;
12 //网址
13 private $url_list = array();
14 //参数
15 private $curl_setopt = array(
16 'CURLOPT_RETURNTRANSFER' => 1, //结果返回给变量
17 'CURLOPT_HEADER' => 0, //要HTTP头不?
18 'CURLOPT_NOBODY' => 0, //不要内容?
19 'CURLOPT_FOLLOWLOCATION' => 0, //不自动跟踪
20 'CURLOPT_TIMEOUT' => 15//超时(s)
21 );
22
23 function __construct($seconds = 30){
24 set_time_limit($seconds);
25 }
26
27 /*
28 * 设置网址
29 * @list 数组
30 */
31
32 public function setUrlList($list = array()){
33 $this->url_list = $list;
34 }
35
36 /*
37 * 设置参数
38 * @cutPot array
39 */
40
41 public function setOpt($cutPot){
42 $this->curl_setopt = $cutPot + $this->curl_setopt;
43 }
44
45 /*
46 * 执行
47 * @return array
48 */
49
50 public function exec(){
51 try{
52 $mh = curl_multi_init();
53 $conn = array();
54 foreach($this->url_list as $i => $url){
55 $conn[$i] = curl_init($url);
56 if(strtolower(substr($url, 0, 5)) == 'https'){
57 curl_setopt($conn[$i], CURLOPT_SSL_VERIFYPEER, false);
58 curl_setopt($conn[$i], CURLOPT_SSL_VERIFYHOST, false);
59 }
60 foreach($this->curl_setopt as $key => $val){
61 curl_setopt($conn[$i], preg_replace('/(CURLOPT_\w{1,})/ie', '$0', $key), $val);
62 }
63 curl_multi_add_handle($mh, $conn[$i]);
64 }
65
66 /* if($i == 0 && isset($conn[$i]))
67 return array(curl_exec($conn[$i])); //一个网址的时候直接执行返回
68 */
69 $active = false;
70
71 do{
72 $mrc = curl_multi_exec($mh, $active);
73 }while($mrc == CURLM_CALL_MULTI_PERFORM);
74
75 //执行状态
76 //$http_code = array();
77 $res = array();
78
79 while($active and $mrc == CURLM_OK){
80 if(curl_multi_select($mh) != -1){//php5.3.18+的某些版本会一直返回-1,请去掉此处的if
81 do{
82 $mrc = curl_multi_exec($mh, $active);
83 }while($mrc == CURLM_CALL_MULTI_PERFORM);
84 //得到线程信息
85 /* $mhinfo = curl_multi_info_read($mh);
86 if(false !== $mhinfo){
87 $chinfo = curl_getinfo($mhinfo['handle']);
88 //$http_code[] = $chinfo['url'] . ' : ' . $chinfo['http_code']; // == 0 || $chinfo['http_code'] == 404;
89 //得到httpCode为200的内容 如果CURLOPT_FOLLOWLOCATION且有Location头标记的话$chinfo['url']会是最后一个URL
90 $res[array_search($chinfo['url'], $this->url_list)] = ($chinfo['http_code'] == 200 ? curl_multi_getcontent($mhinfo['handle']) : 'null');
91 curl_multi_remove_handle($mh, $mhinfo['handle']); //用完马上释放资源
92 curl_close($mhinfo['handle']);
93 } */
94 }
95 }
96 //print_r($http_code);
97 foreach($this->url_list as $i => $url){
98 $status = curl_getinfo($conn[$i], CURLINFO_HTTP_CODE);
99 $res[$i] = (($status == 200 || $status == 302 || $status == 301) ? curl_multi_getcontent($conn[$i]) : null);
100 curl_close($conn[$i]);
101 curl_multi_remove_handle($mh, $conn[$i]); //用完马上释放资源
102 }
103 curl_multi_close($mh);
104 }catch(Exception $e){
105 echo '错误: ' . $e->getMessage();
106 }
107 return $res;
108 }
109
110 }
111
112 ?>