Skip to content

Instantly share code, notes, and snippets.

@ianmustafa
Last active May 8, 2024 13:57
Show Gist options
  • Save ianmustafa/716fbbc2f853429ac1a063457523822e to your computer and use it in GitHub Desktop.
Save ianmustafa/716fbbc2f853429ac1a063457523822e to your computer and use it in GitHub Desktop.
PHP: cURL vs file_get_contents

PHP: cURL vs file_get_contents()

This is a very often asked question among PHP developers. This gist aims to answer it.

Case study

RajaOngkir API.

Benchmark

  • Done in DigitalOcean, cheapest droplet ($5), located in Singapore, using Ubuntu Server 18.04.3. (Referral link for anybody interested.)
  • System configured with (mostly) default configuration, using what's available from the package installation.
  • PHP versions used are the latest version of 7.1, 7.2 and 7.2 as of October 11th 2019, with both CLI and Nginx FPM version being used.
  • Code used in bechmark is down below.

Package Installed

Using help from ppa:ondrej/nginx and ppa:ondrej/php:

$ sudo add-apt-repository -y ppa:ondrej/nginx
$ sudo add-apt-repository -y ppa:ondrej/php
$ sudo apti install -y nginx php7.3-fpm php7.3-cli php7.3-curl \
  php7.2-fpm php7.2-cli php7.2-curl php7.1-fpm php7.1-cli php7.1-curl

Result

Is also down below, in JSON format, done by microtime().

Conclusion

Spoiler!

file_get_contents() is slightly faster than cURL. As it doesn't need any dependency (cURL needs php-curl extension), that's a big win for me.

<?php
// PLEASE: DO NOT ATTEMPT WITH YOUR STARTER KEY!!!
// You will blow your daily limit. Trust me. Pls.
$apiKey = 'your API key';
$repeat = 'cli' === PHP_SAPI
? ($argv[1] ?? 20)
: ($_GET['repeat'] ?? $_GET['n'] ?? $_GET['r'] ?? 20);
function runCurl(string $url, string $method = 'GET', array $payload = []): float
{
global $apiKey;
$start = microtime(true);
$curl = curl_init();
$options = [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 5,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => [
'Key:'.$apiKey,
],
];
if ('POST' === $method) {
$options[CURLOPT_HTTPHEADER][] = 'Content-Type:application/x-www-form-urlencoded';
$options[CURLOPT_POSTFIELDS] = http_build_query($payload);
$options[CURLOPT_CUSTOMREQUEST] = 'POST';
}
curl_setopt_array($curl, $options);
$rawResponse = curl_exec($curl);
curl_close($curl);
return microtime(true) - $start;
}
function runGetContents(string $url, string $method = 'GET', array $payload = []): float
{
global $apiKey;
$start = microtime(true);
$options = [
'max_redirects' => 5,
'timeout' => 30,
'protocol_version' => 1.1,
'method' => 'GET',
'header' => "Key:{$apiKey}\r\n",
];
if ('POST' === $method) {
$options['header'] .= "Content-Type:application/x-www-form-urlencoded\r\n";
$options['content'] = http_build_query($payload);
$options['method'] = 'POST';
}
$context = stream_context_create(['http' => $options]);
file_get_contents($url, false, $context);
return microtime(true) - $start;
}
$dataset = [
'get_provinces' => ['https://api.rajaongkir.com/starter/province'],
'get_cities_by_province' => ['https://api.rajaongkir.com/starter/city?province=6'],
'get_cost' => ['https://api.rajaongkir.com/starter/cost', 'POST', [
'origin' => 501,
'destination' => 80,
'weight' => 1000,
'courier' => 'jne',
]],
];
$result = [];
if ('cli' !== PHP_SAPI) {
header('Content-Type: text/plain');
}
echo 'PHP version: '.PHP_VERSION."\nSAPI: ".PHP_SAPI."\n\n";
foreach ($dataset as $title => $data) {
echo "Run: {$title}\n";
for($i = 1; $i <= $repeat; $i++) {
echo "#{$i}... ";
$result['curl'][$title][] = runCurl(...$data);
$result['file_get_contents'][$title][] = runGetContents(...$data);
echo "Done\n";
}
echo "\n";
}
$render = json_encode($result, JSON_PRETTY_PRINT)."\n";
file_put_contents(__DIR__.'/result-'.PHP_VERSION.('cli'===PHP_SAPI?'-cli':'-nginx-unixsock').'.json', $render);
if ('cli'!==PHP_SAPI) {
echo $render;
}
echo "\n\n";
{
"curl": {
"get_provinces": [
0.1139688491821289,
0.1094350814819336,
0.1129598617553711,
0.10925483703613281,
0.10926413536071777,
0.10904407501220703,
0.10873603820800781,
0.10852384567260742,
0.10533618927001953,
0.10884213447570801,
0.1103811264038086,
0.11229681968688965,
0.10946893692016602,
0.10616683959960938,
0.10461902618408203,
0.10519599914550781,
0.11262917518615723,
0.10864400863647461,
0.10527682304382324,
0.10520195960998535
],
"get_cities_by_province": [
0.10994195938110352,
0.10895085334777832,
0.10908102989196777,
0.10896182060241699,
0.10504508018493652,
0.1054539680480957,
0.11218094825744629,
0.10627412796020508,
0.11289000511169434,
0.11394810676574707,
0.10490584373474121,
0.11009597778320312,
0.10889482498168945,
0.10483312606811523,
0.1091008186340332,
0.1131281852722168,
0.10907697677612305,
0.11321091651916504,
0.1088719367980957,
0.10980105400085449
],
"get_cost": [
0.11506390571594238,
0.11227607727050781,
0.10928010940551758,
0.10860586166381836,
0.11280608177185059,
0.10894918441772461,
0.11320114135742188,
0.11836600303649902,
0.1106119155883789,
0.11228489875793457,
0.10886716842651367,
0.10934782028198242,
0.11299014091491699,
0.11297297477722168,
0.11532187461853027,
0.1087789535522461,
0.11779189109802246,
0.11486697196960449,
0.11226892471313477,
0.1124880313873291
]
},
"file_get_contents": {
"get_provinces": [
0.09813785552978516,
0.10665011405944824,
0.09779214859008789,
0.09850001335144043,
0.09877896308898926,
0.09895586967468262,
0.09933710098266602,
0.09896683692932129,
0.09891390800476074,
0.09865903854370117,
0.1060028076171875,
0.09859299659729004,
0.09857392311096191,
0.09836101531982422,
0.1024789810180664,
0.09878706932067871,
0.09956097602844238,
0.0985710620880127,
0.09859585762023926,
0.09839797019958496
],
"get_cities_by_province": [
0.09843897819519043,
0.09899091720581055,
0.09871697425842285,
0.09881806373596191,
0.09921121597290039,
0.09893393516540527,
0.09840798377990723,
0.09770512580871582,
0.09898900985717773,
0.10195088386535645,
0.09889602661132812,
0.10573697090148926,
0.09895515441894531,
0.09875297546386719,
0.10275793075561523,
0.0986781120300293,
0.09874391555786133,
0.09893107414245605,
0.0986628532409668,
0.10222482681274414
],
"get_cost": [
0.10314297676086426,
0.10286712646484375,
0.10691595077514648,
0.10319399833679199,
0.09919977188110352,
0.09973907470703125,
0.10218310356140137,
0.10024118423461914,
0.10247802734375,
0.10274696350097656,
0.09884405136108398,
0.1035149097442627,
0.10254597663879395,
0.10231304168701172,
0.10042309761047363,
0.10020089149475098,
0.10464787483215332,
0.10305309295654297,
0.10196590423583984,
0.10289597511291504
]
}
}
{
"curl": {
"get_provinces": [
0.1140909194946289,
0.10911893844604492,
0.11261105537414551,
0.10529398918151855,
0.11374497413635254,
0.10853195190429688,
0.10532283782958984,
0.10893917083740234,
0.11758899688720703,
0.11387515068054199,
0.10676813125610352,
0.10540914535522461,
0.10936689376831055,
0.10952210426330566,
0.11654400825500488,
0.10573315620422363,
0.10622096061706543,
0.10820889472961426,
0.10830283164978027,
0.10519194602966309
],
"get_cities_by_province": [
0.10480594635009766,
0.10568499565124512,
0.10995912551879883,
0.10545992851257324,
0.10516786575317383,
0.1160128116607666,
0.1091001033782959,
0.10958504676818848,
0.10917401313781738,
0.10881686210632324,
0.10981893539428711,
0.11030912399291992,
0.10524892807006836,
0.11321711540222168,
0.1095271110534668,
0.1089169979095459,
0.10973882675170898,
0.10817503929138184,
0.10523700714111328,
0.11339092254638672
],
"get_cost": [
0.11253118515014648,
0.10965204238891602,
0.11049985885620117,
0.10898494720458984,
0.10946488380432129,
0.11322379112243652,
0.10930109024047852,
0.10934615135192871,
0.11077094078063965,
0.10962796211242676,
0.11363410949707031,
0.10921478271484375,
0.10938501358032227,
0.1117098331451416,
0.11048507690429688,
0.11260414123535156,
0.11337804794311523,
0.10993599891662598,
0.11768007278442383,
0.11185216903686523
]
},
"file_get_contents": {
"get_provinces": [
0.09864401817321777,
0.09982991218566895,
0.09819722175598145,
0.0995790958404541,
0.10215616226196289,
0.09873509407043457,
0.09892892837524414,
0.09931421279907227,
0.09762310981750488,
0.10507798194885254,
0.09834098815917969,
0.09873485565185547,
0.09875893592834473,
0.09916496276855469,
0.09827113151550293,
0.09831500053405762,
0.09904694557189941,
0.09984898567199707,
0.09868597984313965,
0.09914803504943848
],
"get_cities_by_province": [
0.09896087646484375,
0.09797501564025879,
0.09845399856567383,
0.0985870361328125,
0.09978508949279785,
0.09888505935668945,
0.09844088554382324,
0.0989079475402832,
0.09895706176757812,
0.09916806221008301,
0.09808516502380371,
0.10544800758361816,
0.09861898422241211,
0.09862899780273438,
0.09883594512939453,
0.09899401664733887,
0.10714006423950195,
0.09884285926818848,
0.12196087837219238,
0.09889793395996094
],
"get_cost": [
0.10960102081298828,
0.10326886177062988,
0.10907196998596191,
0.09835600852966309,
0.09895992279052734,
0.1027979850769043,
0.09848380088806152,
0.10502004623413086,
0.10299992561340332,
0.10237812995910645,
0.10641312599182129,
0.10248494148254395,
0.10422182083129883,
0.10379195213317871,
0.10125398635864258,
0.10641694068908691,
0.10649800300598145,
0.10448908805847168,
0.10013318061828613,
0.10407495498657227
]
}
}
{
"curl": {
"get_provinces": [
0.11455202102661133,
0.11269187927246094,
0.10495591163635254,
0.10912394523620605,
0.11287403106689453,
0.10867190361022949,
0.10547804832458496,
0.1050560474395752,
0.10903382301330566,
0.10492396354675293,
0.1049489974975586,
0.10619592666625977,
0.10528397560119629,
0.1051790714263916,
0.11705708503723145,
0.11980795860290527,
0.10938382148742676,
0.10483908653259277,
0.1085500717163086,
0.10904097557067871
],
"get_cities_by_province": [
0.11072802543640137,
0.11013388633728027,
0.12030696868896484,
0.10921716690063477,
0.1087639331817627,
0.1048119068145752,
0.10974597930908203,
0.11318778991699219,
0.10940289497375488,
0.10796880722045898,
0.10910916328430176,
0.10942411422729492,
0.10882401466369629,
0.10904717445373535,
0.10895705223083496,
0.10857701301574707,
0.1091618537902832,
0.10508084297180176,
0.10489702224731445,
0.10602307319641113
],
"get_cost": [
0.11651110649108887,
0.10876607894897461,
0.10946297645568848,
0.1090250015258789,
0.11570000648498535,
0.11647915840148926,
0.11687994003295898,
0.10848498344421387,
0.11368989944458008,
0.11350607872009277,
0.11740493774414062,
0.1129770278930664,
0.11270809173583984,
0.11586284637451172,
0.10808491706848145,
0.10897994041442871,
0.1090390682220459,
0.11356997489929199,
0.1121530532836914,
0.1154940128326416
]
},
"file_get_contents": {
"get_provinces": [
0.09625005722045898,
0.1032102108001709,
0.09862995147705078,
0.09874391555786133,
0.09914398193359375,
0.09889817237854004,
0.09838700294494629,
0.09859490394592285,
0.09897994995117188,
0.09905791282653809,
0.09877896308898926,
0.09749794006347656,
0.09827804565429688,
0.16700005531311035,
0.09936881065368652,
0.0992131233215332,
0.09875106811523438,
0.09945392608642578,
0.10268306732177734,
0.09889483451843262
],
"get_cities_by_province": [
0.1013789176940918,
0.10648512840270996,
0.09858298301696777,
0.09862518310546875,
0.09878301620483398,
0.09913992881774902,
0.09880805015563965,
0.10592198371887207,
0.10341000556945801,
0.0989990234375,
0.09868693351745605,
0.09876012802124023,
0.09844613075256348,
0.09866499900817871,
0.09918498992919922,
0.09900689125061035,
0.0985422134399414,
0.09890103340148926,
0.09883499145507812,
0.10433793067932129
],
"get_cost": [
0.10303401947021484,
0.1027522087097168,
0.1064300537109375,
0.10391497611999512,
0.10746002197265625,
0.10672283172607422,
0.1072549819946289,
0.10462498664855957,
0.1078338623046875,
0.11071395874023438,
0.09873104095458984,
0.0992429256439209,
0.1042790412902832,
0.10369014739990234,
0.10268592834472656,
0.09881019592285156,
0.10264778137207031,
0.10328412055969238,
0.10326409339904785,
0.10698294639587402
]
}
}
{
"curl": {
"get_provinces": [
0.11482787132263184,
0.1054069995880127,
0.11014103889465332,
0.10998106002807617,
0.10977721214294434,
0.12089991569519043,
0.10591816902160645,
0.1091759204864502,
0.10933899879455566,
0.10528707504272461,
0.10900712013244629,
0.11293816566467285,
0.10493111610412598,
0.10796689987182617,
0.10897684097290039,
0.10892987251281738,
0.1094510555267334,
0.1092078685760498,
0.10990285873413086,
0.1130368709564209
],
"get_cities_by_province": [
0.10926318168640137,
0.10921192169189453,
0.10927915573120117,
0.10906505584716797,
0.10592103004455566,
0.11013007164001465,
0.1094059944152832,
0.10886406898498535,
0.10870790481567383,
0.11005282402038574,
0.10534095764160156,
0.11290907859802246,
0.10948395729064941,
0.10953402519226074,
0.10529088973999023,
0.11738181114196777,
0.10941505432128906,
0.108795166015625,
0.11084389686584473,
0.10895204544067383
],
"get_cost": [
0.11131596565246582,
0.10848689079284668,
0.10894894599914551,
0.11272001266479492,
0.11277890205383301,
0.10968303680419922,
0.1135861873626709,
0.10958385467529297,
0.1146390438079834,
0.1124269962310791,
0.11135101318359375,
0.11694097518920898,
0.10895895957946777,
0.10929989814758301,
0.12105393409729004,
0.11690497398376465,
0.10879206657409668,
0.10941600799560547,
0.11130404472351074,
0.10943818092346191
]
},
"file_get_contents": {
"get_provinces": [
0.09836006164550781,
0.09909892082214355,
0.09745216369628906,
0.0982048511505127,
0.10242414474487305,
0.09863686561584473,
0.10192608833312988,
0.09870696067810059,
0.09904098510742188,
0.09874916076660156,
0.10336780548095703,
0.09855318069458008,
0.10004806518554688,
0.09946608543395996,
0.09860491752624512,
0.09887313842773438,
0.09874391555786133,
0.09829401969909668,
0.09834599494934082,
0.09933018684387207
],
"get_cities_by_province": [
0.10283803939819336,
0.09884810447692871,
0.09870100021362305,
0.09820199012756348,
0.09856081008911133,
0.09775400161743164,
0.09891700744628906,
0.09930801391601562,
0.10190010070800781,
0.09872698783874512,
0.09872317314147949,
0.09892511367797852,
0.09825801849365234,
0.09867715835571289,
0.09869003295898438,
0.0985708236694336,
0.09863996505737305,
0.10583114624023438,
0.09894609451293945,
0.09849786758422852
],
"get_cost": [
0.09966707229614258,
0.10273313522338867,
0.10326600074768066,
0.09943413734436035,
0.10267400741577148,
0.10251712799072266,
0.1018519401550293,
0.09836101531982422,
0.10115909576416016,
0.10006880760192871,
0.10073614120483398,
0.10284280776977539,
0.10335087776184082,
0.09828495979309082,
0.1034388542175293,
0.10271000862121582,
0.09906196594238281,
0.10431194305419922,
0.09878802299499512,
0.10427689552307129
]
}
}
{
"curl": {
"get_provinces": [
0.1155390739440918,
0.10902595520019531,
0.11794900894165039,
0.1127479076385498,
0.1093437671661377,
0.10928797721862793,
0.10551118850708008,
0.10498285293579102,
0.1206059455871582,
0.1050729751586914,
0.10470819473266602,
0.1063690185546875,
0.11371612548828125,
0.10907816886901855,
0.10890913009643555,
0.10890603065490723,
0.10838913917541504,
0.10876297950744629,
0.11210083961486816,
0.10852599143981934
],
"get_cities_by_province": [
0.10911107063293457,
0.11430501937866211,
0.10918092727661133,
0.10880804061889648,
0.10896587371826172,
0.11200380325317383,
0.10564899444580078,
0.10872602462768555,
0.11724185943603516,
0.10643291473388672,
0.10867714881896973,
0.10957193374633789,
0.10921406745910645,
0.11029791831970215,
0.11054301261901855,
0.11181402206420898,
0.10895514488220215,
0.10577106475830078,
0.10917091369628906,
0.10941791534423828
],
"get_cost": [
0.10732698440551758,
0.11283016204833984,
0.10867500305175781,
0.11051702499389648,
0.10878491401672363,
0.10930705070495605,
0.10960006713867188,
0.1166238784790039,
0.11312198638916016,
0.1134028434753418,
0.10925793647766113,
0.11294007301330566,
0.10857582092285156,
0.11225509643554688,
0.108734130859375,
0.10912585258483887,
0.10895586013793945,
0.10879397392272949,
0.10909485816955566,
0.11205697059631348
]
},
"file_get_contents": {
"get_provinces": [
0.09851694107055664,
0.09868311882019043,
0.09781479835510254,
0.09889888763427734,
0.09869098663330078,
0.09843707084655762,
0.09835004806518555,
0.0992729663848877,
0.09478902816772461,
0.09883308410644531,
0.0989840030670166,
0.09752678871154785,
0.09795188903808594,
0.09923601150512695,
0.0987999439239502,
0.09919309616088867,
0.09909391403198242,
0.10017704963684082,
0.09980583190917969,
0.10216593742370605
],
"get_cities_by_province": [
0.09944605827331543,
0.10477399826049805,
0.10305905342102051,
0.09859299659729004,
0.10004711151123047,
0.09829401969909668,
0.09874391555786133,
0.09903097152709961,
0.10580897331237793,
0.1019279956817627,
0.09911799430847168,
0.10232400894165039,
0.10649585723876953,
0.10149002075195312,
0.10262513160705566,
0.09922599792480469,
0.09822392463684082,
0.09829497337341309,
0.09827685356140137,
0.09905099868774414
],
"get_cost": [
0.10239911079406738,
0.09872889518737793,
0.09900403022766113,
0.10166788101196289,
0.10255289077758789,
0.09851312637329102,
0.09855294227600098,
0.10225701332092285,
0.10283517837524414,
0.10228490829467773,
0.10241508483886719,
0.09897422790527344,
0.09920310974121094,
0.09954595565795898,
0.1035618782043457,
0.0984811782836914,
0.10285115242004395,
0.09886693954467773,
0.09961605072021484,
0.09876704216003418
]
}
}
{
"curl": {
"get_provinces": [
0.12235808372497559,
0.10933303833007812,
0.10551595687866211,
0.10541701316833496,
0.11606097221374512,
0.1054530143737793,
0.1053469181060791,
0.10532593727111816,
0.10476016998291016,
0.10889291763305664,
0.10956501960754395,
0.10851383209228516,
0.10962200164794922,
0.10978507995605469,
0.11025500297546387,
0.10539102554321289,
0.10976505279541016,
0.11251211166381836,
0.10845112800598145,
0.10582494735717773
],
"get_cities_by_province": [
0.10955595970153809,
0.11185193061828613,
0.10548710823059082,
0.11321306228637695,
0.10576701164245605,
0.10572099685668945,
0.10926198959350586,
0.10923194885253906,
0.11191987991333008,
0.10468006134033203,
0.10593414306640625,
0.1052098274230957,
0.10510587692260742,
0.10946512222290039,
0.10936307907104492,
0.10517501831054688,
0.11029291152954102,
0.10548710823059082,
0.11381793022155762,
0.11725687980651855
],
"get_cost": [
0.10800981521606445,
0.11308598518371582,
0.11174607276916504,
0.1158599853515625,
0.10818791389465332,
0.11384010314941406,
0.11158609390258789,
0.10840415954589844,
0.1091470718383789,
0.11257004737854004,
0.10921692848205566,
0.11515188217163086,
0.1108090877532959,
0.10977506637573242,
0.11156511306762695,
0.10981607437133789,
0.11290478706359863,
0.1089639663696289,
0.10918998718261719,
0.10917019844055176
]
},
"file_get_contents": {
"get_provinces": [
0.0984199047088623,
0.09875702857971191,
0.09828996658325195,
0.0999000072479248,
0.10287094116210938,
0.10225200653076172,
0.09878993034362793,
0.09887909889221191,
0.09536409378051758,
0.09874486923217773,
0.10350704193115234,
0.09810018539428711,
0.10258603096008301,
0.10002899169921875,
0.09560608863830566,
0.09885597229003906,
0.0990300178527832,
0.09924578666687012,
0.09461402893066406,
0.09866189956665039
],
"get_cities_by_province": [
0.09992599487304688,
0.1066749095916748,
0.09884214401245117,
0.09835290908813477,
0.09821605682373047,
0.09829306602478027,
0.0983879566192627,
0.09642791748046875,
0.09903502464294434,
0.10276293754577637,
0.09843015670776367,
0.10251116752624512,
0.09901690483093262,
0.09842705726623535,
0.09870505332946777,
0.09904003143310547,
0.10142087936401367,
0.09830713272094727,
0.09958004951477051,
0.10157489776611328
],
"get_cost": [
0.10638594627380371,
0.10395407676696777,
0.10359001159667969,
0.10764288902282715,
0.0993039608001709,
0.10239100456237793,
0.10103082656860352,
0.09888505935668945,
0.10008120536804199,
0.10223007202148438,
0.09813809394836426,
0.10100793838500977,
0.10165596008300781,
0.10232305526733398,
0.10001683235168457,
0.10294699668884277,
0.0985109806060791,
0.09885311126708984,
0.09889602661132812,
0.09900689125061035
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment