Home > Backend Development > PHP Tutorial > CURL multi-threading does not execute requests all the time

CURL multi-threading does not execute requests all the time

WBOY
Release: 2016-08-04 09:21:10
Original
1315 people have browsed it

The code is as follows
`

ini_set('max_execution_time',0);

for($i = 6055;$i >3055;$i--){

<code>$connomains[] = 'http://www.xxxxx.com/index.php&id='.$i;</code>
Copy after login
Copy after login

}

$mh = curl_multi_init();

foreach ($connomains as $i => $url) {

<code>$conn[$i]=curl_init($url);
curl_setopt($conn[$i],CURLOPT_RETURNTRANSFER,1);
curl_multi_add_handle ($mh,$conn[$i]);</code>
Copy after login
Copy after login

}

$active = null;
do {

<code>$mrc = curl_multi_exec($mh, $active);</code>
Copy after login
Copy after login

} while ($mrc == CURLM_CALL_MULTI_PERFORM);

while ($active && $mrc ​​== CURLM_OK) {

<code>if (curl_multi_select($mh) != -1) {
    do {
        $mrc = curl_multi_exec($mh, $active);
    } while ($mrc == CURLM_CALL_MULTI_PERFORM);
}</code>
Copy after login
Copy after login

}

foreach ($connomains as $i => $url) {

<code>$res[$i]=curl_multi_getcontent($conn[$i]);
$res[$i] = json_decode($res[$i],true);
var_dump($res[$i]);
curl_close($conn[$i]);
</code>
Copy after login
Copy after login

}
print_r($res);

?>
`

The page source address is http://www.4wei.cn/archives/1001877

Reply content:

The code is as follows
`

ini_set('max_execution_time',0);

for($i = 6055;$i >3055;$i--){

<code>$connomains[] = 'http://www.xxxxx.com/index.php&id='.$i;</code>
Copy after login
Copy after login

}

$mh = curl_multi_init();

foreach ($connomains as $i => $url) {

<code>$conn[$i]=curl_init($url);
curl_setopt($conn[$i],CURLOPT_RETURNTRANSFER,1);
curl_multi_add_handle ($mh,$conn[$i]);</code>
Copy after login
Copy after login

}

$active = null;
do {

<code>$mrc = curl_multi_exec($mh, $active);</code>
Copy after login
Copy after login

} while ($mrc == CURLM_CALL_MULTI_PERFORM);

while ($active && $mrc ​​== CURLM_OK) {

<code>if (curl_multi_select($mh) != -1) {
    do {
        $mrc = curl_multi_exec($mh, $active);
    } while ($mrc == CURLM_CALL_MULTI_PERFORM);
}</code>
Copy after login
Copy after login

}

foreach ($connomains as $i => $url) {

<code>$res[$i]=curl_multi_getcontent($conn[$i]);
$res[$i] = json_decode($res[$i],true);
var_dump($res[$i]);
curl_close($conn[$i]);
</code>
Copy after login
Copy after login

}
print_r($res);

?>
`

The page source address is http://www.4wei.cn/archives/1001877

The meaning of the code is to execute the output after requesting 3000 times. Change the number to a smaller value and take a look

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template