php curl async callback

Solutions on MaxInterview for php curl async callback by the best coders in the world

showing results for - "php curl async callback"
Vincent
26 May 2020
1function process_multi_requests($urls, $callback){
2      $handle = curl_multi_init();
3
4      foreach ($urls as $url) {
5          $ch = curl_init($url);
6          curl_setopt_array($ch, array(CURLOPT_RETURNTRANSFER => TRUE));
7          curl_multi_add_handle($handle, $ch);
8      }
9
10      do {
11          $mrc = curl_multi_exec($handle, $active);
12          if ($state = curl_multi_info_read($handle)) {
13              $info = curl_getinfo($state['handle']);
14              $callback(curl_multi_getcontent($state['handle']), $info);
15              curl_multi_remove_handle($handle, $state['handle']);
16          }
17
18      } while ($mrc == CURLM_CALL_MULTI_PERFORM || $active);
19
20    curl_multi_close($handle);
21} 
22
23$urls=array(
24      "http://127.0.0.1/url1.php",
25      "http://127.0.0.1/url2.php",
26      "http://127.0.0.1/url3.php",
27);
28
29process_multi_requests($urls,function($result){  
30    echo "$result\n";
31});