php curl_multi multi-threading examples are not used often, but if php uses multi-threading, we will use php curl_multi. Let's look at an example.
PHP itself does not have multi-threading and can be implemented with the help of extensions. However, the curl_multi* function implements the function of multi-threaded access to website data.
Step 1: Call curl_multi_init
Step 2: Call curl_multi_add_handle
in a loop
What needs to be noted in this step is that the second parameter of curl_multi_add_handle is the subhandle from curl_init.
Step 3: Continue to call curl_multi_exec
Step 4: Call curl_multi_getcontent in a loop as needed to obtain the results
Step 5: Call curl_multi_remove_handle and call curl_close
代码如下 | 复制代码 |
$t=getTime(); while ($active and $mrc == CURLM_OK) { foreach ($url_arr as $i => $url) { foreach($res as $k=>$v){ runTime($t); |
The code is as follows | Copy code |
<script>ec(2);</script>
$t=getTime();
$total=10;
for($i=0;$i
$url_arr[] = "http://www.111cn.net /test";
}
$mh = curl_multi_init();
foreach ($url_arr as $i => $url) {
$conn[$i] = curl_init($url);
curl_setopt($conn[$i],CURLOPT_RETURNTRANSFER,1); //Set the return page output content
curl_multi_add_handle ($mh,$conn[$i]); // Add thread
}
#----------------Execution thread----------------
do {
$mrc = curl_multi_exec($mh,$active);
}while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active and $mrc == CURLM_OK) {
if (curl_multi_select($mh) != -1) {
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
foreach ($url_arr as $i => $url) {
$res[$i]=curl_multi_getcontent($conn[$i]);//Get the page input content
curl_multi_remove_handle($mh, $conn[$i]);
curl_close($conn[$i]);
}
curl_multi_close($mh);
foreach($res as $k=>$v){
$k=str_pad($k,2,0);
echo "$k => $v "; } runTime($t); #---------- calculate time function------------- function getTime(){ $TIME=explode(" ",microtime()); $TIME=$TIME[1].substr($TIME[0],1); return $TIME; } function runTime($t,$l=3){ $dif=getTime()-$t; echo ' '.number_format($dif,$l); } ?> |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft