-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmcurl.class.php
More file actions
189 lines (172 loc) · 4.21 KB
/
mcurl.class.php
File metadata and controls
189 lines (172 loc) · 4.21 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
* A PHP class for doing parallel cURL requests
*
* @author Roland Boon
*/
class Mcurl {
/*
* The max connections per second setting
* Make sure to set this before adding urls
*/
public $max_per_second = 5;
/*
* Enable (TRUE) of disable (FALSE) debug logging
*/
public $debug = FALSE;
/*
* Enable GZIP-compression (both client and server need to support this)
*/
public $gzip = TRUE;
/*
* Used to store curl_multi_init() handles
*/
private $handle = array();
/*
* Used to store individual curl_init() threads
*/
private $threads = array();
public static function factory()
{
return new Mcurl();
}
public function __construct()
{
$this->handle[0] = curl_multi_init();
}
public function __destruct()
{
/*foreach ($this->threads as $thread)
{
curl_multi_remove_handle($this->handle, $thread['handle']);
}*/
for ($i=0; $i<count($this->handle); $i++)
{
curl_multi_close($this->handle[$i]);
}
}
/*
* bool add_curl ( string $url = NULL )
* Add a URL to perform the cURL on
*
* Parameters
* $url
*
* Return values
* Returns TRUE on success or FALSE on failure
*/
public function add_url($url)
{
$handle = curl_init($url);
if ($handle)
{
curl_setopt($handle, CURLOPT_HEADER, false);
curl_setopt($handle, CURLOPT_FAILONERROR, false);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$this->gzip AND curl_setopt($handle, CURLOPT_ENCODING , 'gzip');
/*
* Can the current multi cURL handler hold any more threads?
*/
if (count(end($this->threads)) < $this->max_per_second)
{
$i = count($this->handle) - 1;
curl_multi_add_handle($this->handle[$i], $handle);
}
/*
* Current multi cURL handler passed connections per second limit, create a new one
*/
else
{
$i = count($this->handle);
$this->handle[$i] = curl_multi_init();
curl_multi_add_handle($this->handle[$i], $handle);
}
/*
* Store individual handlers for future reference
*/
$this->threads[$i][] = array(
'url' => $url,
'handle' => $handle,
);
$this->_debug('Added to handler #' . $i . ': ' . $url);
}
else
{
$this->_debug('Failed to add ' . $url);
return false;
}
}
/*
* void execute ()
* Perform all the requests
*
* The do while contains the magic of the max connections per second limit
* First execute multi cURL handler 1, next iteration start 2, and so on
* Each iteration takes 1 second
*/
public function execute()
{
$s = 1;
do {
$active = null;
for ($i=0; $i<min($s, count($this->handle)); $i++)
{
curl_multi_exec($this->handle[$i], $handle_active);
if ($handle_active > 0)
{
$this->_debug('Running handler #' . $i . ' with ' . count($this->threads[$i]) .' threads');
$active = $handle_active;
}
}
sleep(1);
$s++;
} while ($active > 0);
}
/*
* array get_contents ([ string url = FALSE ])
* Retrieve the responses from the cURL requests
*
* Parameters
* $url If provided, only the response of the provided URL will be returned
*
* Return values
* Returns an array with responses in the following format
* array (
* [0] => array (
* 'url' => 'http://example.com/foo'
* 'response' => 'Response of this request
* )
* )
*/
public function get_content($url = FALSE)
{
$return = array();
foreach ($this->threads as $thread)
{
if ($url AND $thread['url'] == $url)
{
$return = curl_multi_getcontent($thread['handle']);
break;
}
else if (!$url)
{
$return[] = array(
'url' => $thread['url'],
'content' => curl_multi_getcontent($thread['handle'])
);
}
}
return $return;
}
/*
* Internal debug method
*/
protected function _debug($message)
{
if ($this->debug)
{
echo date('H:i:s') . ' - '.$message.PHP_EOL;
flush();
}
}
}