Created
May 21, 2014 07:53
-
-
Save spin0us/35b5708cd6cc1ccf38b0 to your computer and use it in GitHub Desktop.
multi cURL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// init multi handler | |
$multihandler = curl_multi_init(); | |
$handlers = $result = array(); | |
... | |
// init each url | |
foreach ($urls as $i) { | |
$handlers[$i] = curl_init($i); | |
curl_setopt($handlers[$i], CURLOPT_RETURNTRANSFER, TRUE); | |
curl_setopt($handlers[$i], CURLOPT_FOLLOWLOCATION, TRUE); // Pour suivre les redirections 302 et 301 de façon transparentes | |
curl_setopt($handlers[$i], CURLOPT_CONNECTTIMEOUT, 10); // Pour réduire la durée d’attente de connexion à 10 secondes | |
curl_setopt($handlers[$i], CURLOPT_TIMEOUT, 30); // Pour réduire la durée d’attente de téléchargement des données à 30 secondes | |
curl_setopt($handlers[$i], CURLOPT_COOKIESESSION, TRUE); // Pour accepter les cookies de sessions | |
curl_multi_add_handle($multihandler, $handlers[$i]); | |
} | |
// exec connexions + download | |
do { | |
curl_multi_exec($multihandler, $pendingConnex); | |
usleep(10000); // 10 ms | |
} while ($pendingConnex > 0); | |
// parse responses | |
foreach ($urls as $i) { | |
$result[] = curl_multi_getcontent($handers[$i]); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment