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 :: Inject interface and not concrete class 
Php :: Failed to open stream: No such file or directory in /home/southsah/public_html/wp-content/advanced-cache.php on line 22 
Php :: composer in serveur ionos 
Php :: CausesActivity trait 
Php :: php vender 403 forbidden 
Php :: how to search like username,email and phone number in php 
Php :: sage theme get template 
Php :: wordpress add menu frontend 
Php :: mysql_query not working in php 7 
Php :: run seeder command in laravel 
Php :: woocommerce create client account without email 
Php :: PHP OOP - Interfaces 
Php :: how to filter the all special characters in cakephp from an array 
Php :: Enqueue WP scripts and styles from a single action hook. 
Php :: php doesnt load updates in css 
Php :: woocommerce affiliate product link image to external link 
Php :: generate hash password in laravel online 
Php :: veue laravel remove #/ 
Php :: tutorial crud phpmyadmin 
Php :: vault create/enable secret engine 
Php :: one to one relationship laravel 
Php :: IlluminateDatabaseQueryException SQLSTATE[HY000]: General error: 3780 Referencing column 
Php :: infoplist codepush key 
Php :: php generate html attributes from array 
Php :: search posts by post title in worpress 
Php :: DB::raw update query in laravel 
Php :: laravel integer data type 
Php :: how to superscript th in php date 
Php :: how to import csv file in laravel 8 
Php :: php make text id attribute safe 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =