-2

I want to grab some text from website with PHP script and I found file_get_contents will cost about 1 second. But it only needs 300ms to download the whole page if i directly open the URL in Chrome ! (same computer same network same URL)

Is there anyone can explain this??

I want to know why file_get_contents is slower than Chrome , not comparing with curl.

yanis
  • 53
  • 1
  • 7
  • 1
    I am guessing file_get_contents needs to resolve additional overheads such as getting a file handle, opening the file, reading the file etc as opposed to a simply http request - which is what the browser-webserver stack was designed to process. – foxbeefly Sep 22 '15 at 08:24
  • possible duplicate of [PHP cUrl vs file\_get\_contents](http://stackoverflow.com/questions/11064980/php-curl-vs-file-get-contents) – pythonian29033 Sep 22 '15 at 08:24
  • Maybe the server is responding in a different way because different headers are sent (Like cache control, compression, encoding, language). You could try to mimic your browser by setting exactly the same headers (http://stackoverflow.com/questions/2107759/php-file-get-contents-and-headers). Do you get the same slow response time when using cURL? – Johni Sep 22 '15 at 09:33
  • I tried cURL with same http head and it cost 300ms each time. – yanis Sep 23 '15 at 03:08

1 Answers1

-1

Chrome use some tricks to speed up it's navigation. For example, when you type an URL in Chrome, Chrome start the request before you hit the enter key. You can try it with a simple server code by printing "Hello World" in the console.

If you want to compare the request duration between 2 methods, use your own server code and log the requests durations (Apache can do that).

Magus
  • 14,796
  • 3
  • 36
  • 51