Demo entry 6677909

curl_multi function

   

Submitted by Synkc on Dec 04, 2017 at 19:50
Language: PHP. Code size: 804 Bytes.

<?php
function fetch_multi_url( $urls, $timeout = 5 ){

	$mh = curl_multi_init();
	$conn = array();

	foreach( $urls as $i => $url) {
		$conn[$i] = curl_init( $url );
		curl_setopt( $conn[$i], CURLOPT_RETURNTRANSFER, true);
		curl_setopt( $conn[$i], CURLOPT_SSL_VERIFYPEER, false );
		curl_setopt( $conn[$i], CURLOPT_SSL_VERIFYHOST, false );

		if ( $timeout ){
		  curl_setopt( $conn[$i], CURLOPT_TIMEOUT, $timeout );
		}

		curl_multi_add_handle( $mh, $conn[$i] );
	}

	$health = null;
	
	do {
		usleep( 10000 );
		$mrc = curl_multi_exec( $mh, $health );
	} while( $health > 0 );

	$res = array();
	foreach( $urls as $i => $url ){
		$res[] = curl_multi_getcontent( $conn[$i] );
		curl_multi_remove_handle( $mh, $conn[$i] );
	}
	
	curl_multi_close($mh);
	return $res;
}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).