Home > php教程 > PHP源码 > php curl_multi multi-threaded query example

php curl_multi multi-threaded query example

WBOY
Release: 2016-07-06 13:34:27
Original
1270 people have browsed it

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<$total;$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<$total;$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); } ?>
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template