class
CurlMultiUtil {
private
static
function
getCurlObject(
$url
,
$postData
=
array
(),
$header
=
array
()){
$options
=
array
();
$url
= trim(
$url
);
$options
[CURLOPT_URL] =
$url
;
$options
[CURLOPT_TIMEOUT] = 3;
$options
[CURLOPT_RETURNTRANSFER] = true;
foreach
(
$header
as
$key
=>
$value
){
$options
[
$key
] =
$value
;
}
if
(!
empty
(
$postData
) &&
is_array
(
$postData
)){
$options
[CURLOPT_POST] = true;
$options
[CURLOPT_POSTFIELDS] = http_build_query(
$postData
);
}
if
(
stripos
(
$url
,'https') === 0){
$options
[CURLOPT_SSL_VERIFYPEER] = false;
}
$ch
= curl_init();
curl_setopt_array(
$ch
,
$options
);
return
$ch
;
}
private
static
function
request(
$chList
){
$downloader
= curl_multi_init();
foreach
(
$chList
as
$ch
){
curl_multi_add_handle(
$downloader
,
$ch
);
}
$res
=
array
();
do
{
while
((
$execrun
= curl_multi_exec(
$downloader
,
$running
)) == CURLM_CALL_MULTI_PERFORM);
if
(
$execrun
!= CURLM_OK) {
break
;
}
while
(
$done
= curl_multi_info_read(
$downloader
)){
$output
= curl_multi_getcontent(
$done
['handle']);
$res
[] =
$output
;
curl_multi_remove_handle(
$downloader
,
$done
['handle']);
}
if
(
$running
) {
$rel
= curl_multi_select(
$downloader
, 1);
if
(
$rel
== -1){
usleep(1000);
}
}
if
(
$running
== false){
break
;
}
}
while
(true);
curl_multi_close(
$downloader
);
return
$res
;
}
public
static
function
get(
$urlArr
){
$data
=
array
();
if
(!
empty
(
$urlArr
)) {
$chList
=
array
();
foreach
(
$urlArr
as
$key
=>
$url
) {
$chList
[] = self::getCurlObject(
$url
);
}
$data
= self::request(
$chList
);
}
return
$data
;
}
}