-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiHttpRequest.php
More file actions
124 lines (112 loc) · 3.23 KB
/
MultiHttpRequest.php
File metadata and controls
124 lines (112 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**
一个简单的多线程采集例子 Curl 多线程类 http://blog.eiodesign.com/archives/86
* 使用方法:
*
if ($arrayimageurl) {
set_time_limit (0);
$arrayimageurl = array_unique($arrayimageurl);
$attachs = [];
//图片大于6张,分组采集
if (count($arrayimageurl) > 6) {
$oldimglist = array_chunk($arrayimageurl, 6,true);
$mpurl = new \com\MultiHttpRequest();
foreach ($oldimglist as $key=>$value) {
$mpurl->set_urls($value);
$images = $mpurl->start();
if (is_array($images)) {
foreach ($images as $image_key => $image_value) {
if (!empty($image_value)) {
$attach = model('Upload','logic')->upload($image_value,['imgUrl'=>$image_key],'dwonFile')->getFileInfo();
if ($attach['state'] == 'SUCCESS') {
$imagereplace['oldimageurl'][] = $image_key;
$imagereplace['newimageurl'][] = $attach['url'];
$attachs[] = $attach;
}
}
unset($image_key,$image_value);
}
}
}
} else {
$mpurl = new \com\MultiHttpRequest();
$mpurl->set_urls($arrayimageurl);
$images = $mpurl->start();
if (is_array($images)) {
foreach ($images as $image_key => $image_value) {
if (!empty($image_value)) {
$attach = model('Upload','logic')->upload($image_value,['imgUrl'=>$image_key],'dwonFile')->getFileInfo();
if ($attach['state'] == 'SUCCESS') {
$imagereplace['oldimageurl'][] = $image_key;
$imagereplace['newimageurl'][] = $attach['url'];
$attachs[] = $attach;
}
}
unset($image_key,$image_value);
}
}
}
}
*/
namespace com;
class MultiHttpRequest {
public $urls = array();
public $curlopt_header = 0;
public $method = "GET";
function __construct($urls = false) {
$this->urls = $urls;
}
function set_urls($urls) {
$this->urls = $urls;
return $this;
}
function is_return_header($b) {
$this->curlopt_header = $b;
return $this;
}
function set_method($m) {
$this->medthod = strtoupper($m);
return $this;
}
function start() {
if(!is_array($this->urls) or count($this->urls) == 0){
return false;
}
$curl = $text = array();
// 创建批处理cURL句柄
$handle = curl_multi_init();
//增加和设置句柄
foreach($this->urls as $k=>$v){
$curl[$k] = $this->add_handle($handle, $v);
}
// 执行批处理句柄
$this->exec_handle($handle);
foreach($this->urls as $k=>$v){
$text[$k] = curl_multi_getcontent($curl[$k]);
curl_multi_remove_handle($handle, $curl[$k]);
}
curl_multi_close($handle);
return $text;
}
private function add_handle($handle, $url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, $this->curlopt_header);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_multi_add_handle($handle, $curl);
return $curl;
}
private function exec_handle($handle) {
$flag = null;
do {
curl_multi_exec($handle, $flag);
} while ($flag > 0);
}
public function get_content($url){
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,10);
return curl_exec($ch);
}
}