PHP

PHP: Curl vs file_get_contents benchmark

I benchmarked curl vsĀ file_get_contents in getting getting headers only and returning the HTTP Status Code.

Here are the results:

kevinkivi@server:~/my/secret/directory$ php curlvsfgctest.php
Testing curl speed

Domain: http://google.com
Status: 301
Domain: http://yahoo.com
Status: 301
Domain: http://nytimes.com
Status: 301
Domain: http://theguardian.com
Status: 301
Domain: http://wikipedia.org
Status: 301

Curl speed was 0.35739207267761


Testing file_get_contents speed

Domain: http://google.com
Status: HTTP/1.0 301 Moved Permanently
Domain: http://yahoo.com
Status: HTTP/1.0 301 Moved Permanently
Domain: http://nytimes.com
Status: HTTP/1.1 301 Moved Permanently
Domain: http://theguardian.com
Status: HTTP/1.1 301
Domain: http://wikipedia.org
Status: HTTP/1.1 301 TLS Redirect

file_get_contents was 1.7153549194336

Below is the source code:

Closing statement: Curl is almost 5 times faster. Use Curl unless you cant.

Kevin Kivi

http://kevinkivi.com

I am a professional software developer. My software interests include PHP, Perl, React Native, Node.js and tinkering with anything Linux related. I also have front-end knowledge.

Among other things, on my spare time I do ultra-running, swimming, software developing for fun and watch Mr. Robot.