Skip to content

Instantly share code, notes, and snippets.

@sergigp
Last active January 25, 2021 10:12
Show Gist options
  • Save sergigp/5504930 to your computer and use it in GitHub Desktop.
Save sergigp/5504930 to your computer and use it in GitHub Desktop.
small benchmark between curl, multicurl and file_get_contents. Results: CURL: 7.37 s // MULTI CURL: 1.93 s // FILE GET CONTENTS: 25.94 s
<?php
$data = array(
'http://www.marca.com',
'http://www.wowebook.be',
'http://www.meneame.net',
'http://www.google.com',
'http://www.elpais.es',
'http://www.reddit.com',
'http://www.digg.com',
'http://www.elmundo.es',
'http://www.itbooks.com',
'http://www.as.com',
'http://www.sport.es',
'http://www.elmundodeportivo.es'
);
/*******************************************************/
/************************* CURL ************************/
/*******************************************************/
$first = microtime(true);
foreach ($data as $url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content[] = curl_exec($ch);
curl_close($ch);
}
$total = microtime(true) - $first;
echo "CURL: total " . $total . " s";
/*******************************************************/
/********************** MULTI CURL *********************/
/*******************************************************/
$first = microtime(true);
// array of curl handles
$curly = array();
// data to be returned
$result = array();
// multi handle
$mh = curl_multi_init();
foreach ($data as $id => $d) {
$curly[$id] = curl_init();
curl_setopt($curly[$id], CURLOPT_URL, $url);
curl_setopt($curly[$id], CURLOPT_HEADER, 0);
curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);
if (is_array($d)) {
if (!empty($d['post'])) {
curl_setopt($curly[$id], CURLOPT_POST, 1);
curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']);
}
}
curl_multi_add_handle($mh, $curly[$id]);
}
// execute the handles
$running = null;
do {
curl_multi_exec($mh, $running);
} while($running > 0);
// get content and remove handles
foreach($curly as $id => $c) {
$result[$id] = curl_multi_getcontent($c);
curl_multi_remove_handle($mh, $c);
}
curl_multi_close($mh);
$total = microtime(true) - $first;
echo "MULTI CURL: total " . $total . ' s';
/*******************************************************/
/****************** FILE GET CONTENTS ******************/
/*******************************************************/
$first = microtime(true);
foreach ($data as $url) {
$content[] = file_get_contents($url);
}
$total = microtime(true) - $first;
echo "FILE GET CONTENTS: total " . $total . " s";
?>
@cyaxaress
Copy link

cyaxaress commented Jan 25, 2021

you made a mistake at line 49!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment