简体   繁体   中英

How to echo result since one single curl request is completed via PHP-cUrl multiple-thread

In test.html , demo word included only。

    demo

curl.php

<php?

//init curl
$chArr=[];
for($i=0;$i<500;$i++){
    $chArr[$i]=curl_init("http://dev.site/test.html");
    curl_setopt($chArr[$i],CURLOPT_RETURNTRANSFER,1);
}

//create curl
$mh = curl_multi_init(); 
foreach($chArr as $k => $ch){
    curl_multi_add_handle($mh,$ch); 
}

$running = null;

do{
    curl_multi_exec($mh,$running); 
}while($running > 0); 

//start multi-thread
foreach($chArr as $k => $ch){
    $result[$k]= curl_multi_getcontent($ch);  

    //and echo out the result
    echo "$result[$k]\n"      

    curl_multi_remove_handle($mh,$ch);
}

curl_multi_close($mh); 

I've set 500 requests,about 10s later got the output and there is no response during processing 没有反应


Is there anyway to display the result and give me a Feedback since every single curl request is done,just like below?

在此处输入图片说明


PS:if PHP is hard to do this so,I could accept Nodejs,Python etc.

You may need to use ob_start() and ob_end_flush() .

ob_start() turns on the output buffering, so put that at the top of the for loop before echoing the output.

and

ob_end_flush() sends the output buffer and turn off output buffering.

So your code might look like this,

foreach($chArr as $k => $ch){
    ob_start();
    $result[$k]= curl_multi_getcontent($ch);  

    //and echo out the result
    echo "$result[$k]\n"      
    ob_end_flush();
    curl_multi_remove_handle($mh,$ch);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM