1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MultiHttp; |
4
|
|
|
|
5
|
|
|
use MultiHttp\Exception\InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* |
9
|
|
|
* @author [email protected] [email protected] |
10
|
|
|
* Date: 2017/6/9 |
11
|
|
|
* Time: 15:09 |
12
|
|
|
* @version $Id: $ |
13
|
|
|
* @since 1.0 |
14
|
|
|
* @copyright Sina Corp. |
15
|
|
|
*/ |
16
|
|
|
class MultiRequest |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
<<<<<<< HEAD |
|
|
|
|
19
|
|
|
======= |
20
|
|
|
/** |
21
|
|
|
* @var [Response] |
22
|
|
|
*/ |
23
|
|
|
>>>>>>> 1799bc479182f58431deae9f8975bf80f1e7cee6 |
24
|
|
|
protected static $requestPool = array(); |
25
|
|
|
protected static $multiHandler; |
26
|
|
|
private static $instance; |
27
|
|
|
|
28
|
|
|
protected function __construct() |
29
|
1 |
|
{ |
30
|
|
|
} |
31
|
1 |
|
|
32
|
|
|
public static function create() |
33
|
|
|
{ |
34
|
|
|
if (!(self::$instance instanceof self)) { |
35
|
|
|
self::$instance = new self; |
36
|
1 |
|
} |
37
|
|
|
self::prepare(); |
38
|
1 |
|
return self::$instance; |
39
|
1 |
|
} |
40
|
1 |
|
// protected $isLazy = false; |
41
|
1 |
|
// public function lazyStart(){ |
42
|
1 |
|
// $this->isLazy = true; |
43
|
|
|
// } |
44
|
|
|
// public function lazyEnd(){ |
45
|
|
|
// $this->isLazy = false; |
46
|
|
|
// return $this->execute(); |
47
|
|
|
// } |
48
|
|
|
protected function prepare() |
49
|
1 |
|
{ |
50
|
1 |
|
// if(is_null(self::$multiHandler) || !$this->isLazy){ |
51
|
1 |
|
self::$multiHandler = curl_multi_init(); |
52
|
|
|
// } |
53
|
1 |
|
} |
54
|
|
|
|
55
|
1 |
|
public function add($method, $uri, $payload, array $options = array()) |
56
|
1 |
|
{ |
57
|
|
|
$options = array( |
58
|
|
|
'method' => $method, |
59
|
|
|
'url' => $uri, |
60
|
|
|
'data' => $payload, |
61
|
|
|
) + $options; |
62
|
|
|
$this->addOptions(array($options)); |
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
1 |
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
1 |
|
* @param array $URLOptions |
69
|
1 |
|
* example: array(array('url'=>'http://localhost:9999/','timeout'=>1, 'method'=>'POST', 'data'=>'aa=bb&c=d')) |
70
|
1 |
|
* @return $this |
71
|
1 |
|
*/ |
72
|
1 |
|
public function addOptions(array $URLOptions) |
73
|
1 |
|
{ |
74
|
|
|
foreach ($URLOptions as $options) { |
75
|
|
|
$request = Request::create()->addOptions($options)->applyOptions(); |
76
|
|
|
if (isset($options['callback'])) { |
77
|
|
|
$request->onEnd($options['callback']); |
78
|
|
|
} |
79
|
|
|
$this->import($request); |
80
|
|
|
} |
81
|
1 |
|
return $this; |
82
|
|
|
} |
83
|
1 |
|
|
84
|
1 |
|
public function import(Request $request) |
85
|
1 |
|
{ |
86
|
1 |
|
if (!is_resource($request->curlHandle)) { |
87
|
1 |
|
throw new InvalidArgumentException('Request curl handle is not initialized'); |
88
|
1 |
|
} |
89
|
1 |
|
curl_multi_add_handle(self::$multiHandler, $request->curlHandle); |
90
|
1 |
|
self::$requestPool[] = $request; |
91
|
1 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return array(Response) |
96
|
|
|
*/ |
97
|
|
|
public function execute() |
98
|
1 |
|
{ |
99
|
|
|
// if($this->isLazy) return array(); |
100
|
1 |
|
$sleepTime = 1000;//microsecond, prevent CPU 100% |
101
|
|
|
do { |
102
|
|
|
curl_multi_exec(self::$multiHandler, $active); |
103
|
1 |
|
// bug in PHP 5.3.18+ where curl_multi_select can return -1 |
104
|
1 |
|
// https://bugs.php.net/bug.php?id=63411 |
105
|
1 |
|
if (curl_multi_select(self::$multiHandler) == -1) { |
106
|
|
|
usleep($sleepTime); |
107
|
|
|
} |
108
|
|
|
usleep($sleepTime); |
109
|
|
|
} while ($active); |
110
|
|
|
$return = array(); |
111
|
1 |
|
foreach (self::$requestPool as $request) { |
112
|
|
|
$response = $request->makeResponse(true); |
113
|
1 |
|
$func = $response->request->endCallback(); |
114
|
|
|
if (isset($func)) { |
115
|
1 |
|
$func($response); |
116
|
|
|
} |
117
|
|
|
$return[] = $response; |
118
|
1 |
|
curl_multi_remove_handle(self::$multiHandler, $request->curlHandle); |
119
|
1 |
|
curl_close($request->curlHandle); |
120
|
1 |
|
} |
121
|
1 |
|
curl_multi_close(self::$multiHandler); |
122
|
1 |
|
self::$requestPool = array(); |
123
|
1 |
|
self::$multiHandler = null; |
124
|
1 |
|
return $return; |
125
|
1 |
|
} |
126
|
1 |
|
|
127
|
|
|
} |
128
|
|
|
|