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
|
<?php
function
multiCurl(
$res
,
$options
=
""
){
if
(
count
(
$res
)<=0)
return
False;
$handles
=
array
();
if
(!
$options
)
// add default options
$options
=
array
(
CURLOPT_HEADER=>0,
CURLOPT_RETURNTRANSFER=>1,
);
// add curl options to each handle
foreach
(
$res
as
$k
=>
$row
){
$ch
{
$k
} = curl_init();
$options
[CURLOPT_URL] =
$row
[
'url'
];
curl_setopt_array(
$ch
{
$k
},
$options
);
$handles
[
$k
] =
$ch
{
$k
};
}
$mh
= curl_multi_init();
foreach
(
$handles
as
$k
=>
$handle
){
curl_multi_add_handle(
$mh
,
$handle
);
//echo "<br>adding handle {$k}";
}
$running_handles
= null;
//execute the handles
$cme
=null;
$status
=null;
do
{
$status_cme
= curl_multi_exec(
$mh
,
$running_handles
);
}
while
(
$cme
== CURLM_CALL_MULTI_PERFORM);
while
(
$running_handles
&&
$status_cme
== CURLM_OK) {
if
(curl_multi_select(
$mh
) != -1) {
do
{
$status_cme
= curl_multi_exec(
$mh
,
$running_handles
);
// echo "<br>''threads'' running = {$running_handles}";
}
while
(
$status
== CURLM_CALL_MULTI_PERFORM);
}
}
foreach
(
$res
as
$k
=>
$row
){
$res
[
$k
][
'error'
] = curl_error(
$handles
[
$k
]);
if
(!
empty
(
$res
[
$k
][
'error'
]))
$res
[
$k
][
'data'
] =
''
;
else
$res
[
$k
][
'data'
] = curl_multi_getcontent(
$handles
[
$k
] );
// get results
// close current handler
curl_multi_remove_handle(
$mh
,
$handles
[
$k
] );
}
curl_multi_close(
$mh
);
return
$res
;
// return response
}
$res
=
array
(
"11"
=>
array
(
"url"
=>
"http://api.211.100.56.140.xip.io/shows.json/263?appKey=strKfLS2Hecp"
),
"12"
=>
array
(
"url"
=>
"http://api.211.100.56.140.xip.io/shows.json/263?appKey=strKfLS2Hecp"
),
);
print_r(multiCurl(
$res
));
?>
|
本文转自birdinroom 51CTO博客,原文链接:http://blog.51cto.com/birdinroom/1550527,如需转载请自行联系原作者