Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php curl asyc

function process_multi_requests($urls, $callback){
      $handle = curl_multi_init();

      foreach ($urls as $url) {
          $ch = curl_init($url);
          curl_setopt_array($ch, array(CURLOPT_RETURNTRANSFER => TRUE));
          curl_multi_add_handle($handle, $ch);
      }

      do {
          $mrc = curl_multi_exec($handle, $active);
          if ($state = curl_multi_info_read($handle)) {
              $info = curl_getinfo($state['handle']);
              $callback(curl_multi_getcontent($state['handle']), $info);
              curl_multi_remove_handle($handle, $state['handle']);
          }

      } while ($mrc == CURLM_CALL_MULTI_PERFORM || $active);

    curl_multi_close($handle);
} 

//usage example
$urls=array(
      "http://127.0.0.1/url1.php",
      "http://127.0.0.1/url2.php",
      "http://127.0.0.1/url3.php",
);

$GLOBALS['my_results']=[];
process_multi_requests($urls,function($result){  
 	$GLOBALS['my_results'][]=$result;
  	echo $result."
";//called when the singe request is done
});
//this runs after all requests are done
print_r($GLOBALS['my_results']);//will contain all the results
Comment

PREVIOUS NEXT
Code Example
Php :: php test if three values are equal 
Php :: laravel with has 
Php :: how to get the number of days in the current month using carbon 
Php :: str_replace php 
Php :: return response not found laravel api response 
Php :: [InvalidArgumentException] Package mongodb/mongodb has requirements incompatible with your PHP version , PHP extensions and Composer version: - mongodb/mongodb 1.12.0 requires ext-mongodb ^1.13.0 but it is not prese nt. 
Php :: where not null in laravel 
Php :: laravel run seeder enter timestamps 
Php :: how to get a particular column in laravel 8 
Php :: php mail function from name 
Php :: check value falls between in two range in php 
Php :: php timer 
Php :: laravel required_with 
Php :: convert array to string laravel 
Php :: upload webp to wordpress 
Php :: upgrade php linux 
Php :: http error 500 - php file 
Php :: laravel datatable format date column 
Php :: install laravel 
Php :: get value from json laravel 
Php :: php check if input is int 
Php :: laravel set config value dynamically 
Php :: get all category custom post type wordpress dev 
Php :: php isset post 
Php :: laravel translate 
Php :: get value by today yesterday in laravel 
Php :: php sort associative array by specific value 
Php :: get id php 
Php :: Undefined index: id 
Php :: part of url php 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =