Skip to content

Instantly share code, notes, and snippets.

@spin0us
Created May 21, 2014 07:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spin0us/35b5708cd6cc1ccf38b0 to your computer and use it in GitHub Desktop.
Save spin0us/35b5708cd6cc1ccf38b0 to your computer and use it in GitHub Desktop.
multi cURL
// 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