search
Homephp教程PHP源码php curl_multi multi-threaded query example

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.

The steps are summarized as follows:


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

for each word handle Step 6: Call curl_multi_close

 代码如下 复制代码

$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); // 设置返回页面输出内容
 curl_multi_add_handle ($mh,$conn[$i]); // 添加线程
}
#----------------执行线程----------------
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]);//得到页面输入内容
 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);
}
?>

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); } ?>
Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)