Maison > Article > développement back-end > À propos de la requête simultanée asynchrone php curl http
Recommandation : "Tutoriel vidéo PHP"
Examinons d'abord le code de synchronisation et l'heure de la demande.
$start_time=date("h:i:sa"); for ($i=0; $i <100 ; $i++) { $urls[]="http://www.downxia.com/downinfo/2315".$i.".html"; GetTitle(geturl("http://www.downxia.com/downinfo/2315".$i.".html")); } function geturl($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); return $output; } function GetTitle($output){ preg_match('/<title>.*<\/title>/i',$output,$matches); var_dump($matches[0]); } $end_time=date("h:i:sa"); echo '开始时间是:'.$start_time; echo '结束时间是:'.$end_time;
Vous pouvez voir en bas que le temps a pris 27 secondes.
Ensuite, examinons le code et le temps passé sur les requêtes http simultanées asynchrones de php curl.
$start_time=date("h:i:sa"); $urls=[]; for ($i=0; $i <100 ; $i++) { $urls[]="http://www.downxia.com/downinfo/2315".$i.".html"; } var_dump($urls); // GetTitle('klasjdkla<title>313asds12</title>'); rolling_curl($urls,'GetTitle'); function GetTitle($output){ preg_match('/<title>.*<\/title>/i',$output,$matches); var_dump($matches[0]); } $end_time=date("h:i:sa"); echo '开始时间是:'.$start_time; echo '结束时间是:'.$end_time; function rolling_curl($urls, $callback, $custom_options = null) {//多个url访问 // make sure the rolling window isn't greater than the # of urls $rolling_window = 5; $rolling_window = (sizeof($urls) < $rolling_window) ? sizeof($urls) : $rolling_window; $master = curl_multi_init(); $curl_arr = array(); // add additional curl options here $std_options = array( CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => 5 ); $options = ($custom_options) ? ($std_options + $custom_options) : $std_options; // start the first batch of requests for ($i = 0; $i < $rolling_window; $i++) { $ch = curl_init(); $options[CURLOPT_URL] = $urls[$i]; curl_setopt_array($ch, $options); curl_multi_add_handle($master, $ch); } do { while (($execrun = curl_multi_exec($master, $running)) == CURLM_CALL_MULTI_PERFORM); if ($execrun != CURLM_OK) { break; } // a request was just completed -- find out which one while ($done = curl_multi_info_read($master)) { $info = curl_getinfo($done['handle']); if ($info['http_code'] == 200) { $output = curl_multi_getcontent($done['handle']); // request successful. process output using the callback function. $callback($output); // start a new request (it's important to do this before removing the old one) $ch = curl_init(); $options[CURLOPT_URL] = $urls[$i++]; // increment i curl_setopt_array($ch, $options); curl_multi_add_handle($master, $ch); // remove the curl handle that just completed curl_multi_remove_handle($master, $done['handle']); } else { // request failed. add error handling. } } } while ($running); curl_multi_close($master); return true; }
Ça n'a pris que 3 secondes ? En fait, je pense que cela a pris 5 secondes, car le démarrage est plus lent que la synchronisation, et il est resté bloqué 2 secondes au début.
Efficacité des requêtes http, il ne fait aucun doute que l'asynchrone est bien supérieur au synchrone.
Le code de la requête principale est le suivant : (Ceci est écrit par un étranger, il y a un léger problème, la dernière invite est un décalage indéfini)
function rolling_curl($urls, $callback, $custom_options = null) {//多个url访问 // make sure the rolling window isn't greater than the # of urls $rolling_window = 5; $rolling_window = (sizeof($urls) < $rolling_window) ? sizeof($urls) : $rolling_window; $master = curl_multi_init(); $curl_arr = array(); // add additional curl options here $std_options = array( CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => 5 ); $options = ($custom_options) ? ($std_options + $custom_options) : $std_options; // start the first batch of requests for ($i = 0; $i < $rolling_window; $i++) { $ch = curl_init(); $options[CURLOPT_URL] = $urls[$i]; curl_setopt_array($ch, $options); curl_multi_add_handle($master, $ch); } do { while (($execrun = curl_multi_exec($master, $running)) == CURLM_CALL_MULTI_PERFORM); if ($execrun != CURLM_OK) { break; } // a request was just completed -- find out which one while ($done = curl_multi_info_read($master)) { $info = curl_getinfo($done['handle']); if ($info['http_code'] == 200) { $output = curl_multi_getcontent($done['handle']); // request successful. process output using the callback function. $callback($output); // start a new request (it's important to do this before removing the old one) $ch = curl_init(); $options[CURLOPT_URL] = $urls[$i++]; // increment i curl_setopt_array($ch, $options); curl_multi_add_handle($master, $ch); // remove the curl handle that just completed curl_multi_remove_handle($master, $done['handle']); } else { // request failed. add error handling. } } } while ($running); curl_multi_close($master); return true; }
Modifiez-le. Ajoutez simplement un jugement lors de l'ajout d'une nouvelle URL. // Lorsque $i est égal à la taille du tableau $urls, il n'est pas nécessaire de l'augmenter.
function rolling_curl($urls, $callback, $custom_options = null) {//多个url访问 // make sure the rolling window isn't greater than the # of urls $rolling_window = 5; $rolling_window = (sizeof($urls) < $rolling_window) ? sizeof($urls) : $rolling_window; $master = curl_multi_init(); $curl_arr = array(); // add additional curl options here $std_options = array( CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => 5 ); $options = ($custom_options) ? ($std_options + $custom_options) : $std_options; // start the first batch of requests for ($i = 0; $i < $rolling_window; $i++) { $ch = curl_init(); $options[CURLOPT_URL] = $urls[$i]; curl_setopt_array($ch, $options); curl_multi_add_handle($master, $ch); } do { while (($execrun = curl_multi_exec($master, $running)) == CURLM_CALL_MULTI_PERFORM); if ($execrun != CURLM_OK) { break; } // a request was just completed -- find out which one while ($done = curl_multi_info_read($master)) { $info = curl_getinfo($done['handle']); if ($info['http_code'] == 200) { $output = curl_multi_getcontent($done['handle']); // request successful. process output using the callback function. $callback($output); // start a new request (it's important to do this before removing the old one) // 当$i等于$urls数组大小时不用再增加了 if($i<sizeof($urls)){ $ch = curl_init(); $options[CURLOPT_URL] = $urls[$i++]; // increment i curl_setopt_array($ch, $options); curl_multi_add_handle($master, $ch); } // remove the curl handle that just completed curl_multi_remove_handle($master, $done['handle']); } else { // request failed. add error handling. } } } while ($running); curl_multi_close($master); return true; }
Ce qui précède, la fin. Écrivez-le pour ne pas l’oublier.
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!