Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php curl async callback

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

php async curl request

/* to send a non blocking/async request when you don't need response
one apprach is to just call exec in the background like so: */
$url="https://myurl.com/test.php";
exec("curl '$url' >/dev/null 2>&1 &");

//note: If you are sending lots of requests use curl_multi_init instead
Comment

PREVIOUS NEXT
Code Example
Php :: format money with commas in php 
Php :: laravel wherehas with condition 
Php :: php loop through array of objects 
Php :: php needle haystack 
Php :: make select element readonly 
Php :: laravel vue build production 
Php :: php remove last 3 letters from string 
Php :: php sql query where in array 
Php :: how add field to table by another migration in laravel 
Php :: create laravel 9 auth 
Php :: concat function using laravel update query 
Php :: laravel object to array 
Php :: how validate if one parameter is exist another parameter must exist in laravel 
Php :: how set field after another field in migration in laravel 
Php :: laravel hasmany count 
Php :: get the current date and time in php 
Php :: php shell command execution 
Php :: for each loop syntax in laravel 
Php :: yyyymmdd to yyyy-mm-dd php 
Php :: php get tempfile 
Php :: get last letter in php 
Php :: hmtl remove tag php 
Php :: Installation request for phpoffice/phpspreadsheet 1.4.0 - satisfiable by phpoffice/phpspreadsheet[1.4.0] 
Php :: twig get array key name 
Php :: how to display image in wordpress 
Php :: html_entity_decode (PHP 4 = 4.3.0, PHP 5, PHP 7, PHP 8) html_entity_decode — Convert HTML entities to their corresponding characters 
Php :: check variable type in php 
Php :: laravel session 
Php :: PHP scandir() Function 
Php :: php laravel intervention base64 to image save 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =