开发者

Async requests in PHP

开发者 https://www.devze.com 2023-02-20 00:59 出处:网络
I need to generate requests to several APIs, get response from them and then generate a report. something like this:

I need to generate requests to several APIs, get response from them and then generate a report.

something like this:

foreach($api_array as $api){
    echo $api;

    $responce = file_get_contents($api);

    if($responce) 
        echo 'ok <br/>';
    else 
        echo 'fail <br/>';
}

It's obvious that when run consistently, one by one, this will take A LOT of time to wait for each开发者_如何学C service to respond.

Can this be done asynchronously, like in JavaScript? Thanks a lot!


You can use curl_multi for this.


Yes you can do that using curl_multi that will do it in parallel. You can also use a callback to get the reponses like in this example http://curl.haxx.se/libcurl/php/examples/callbacks.html

Also read more on curl_multi here http://php.net/manual/en/function.curl-multi-init.php2

0

精彩评论

暂无评论...
验证码 换一张
取 消