0

This isn't in the php documentation on php.net - so I asked here.

If I were (for example) to use $contents = file_get_contents("www.bbc.co.uk/news") while www.bbc.co.uk/news was offline, what would file_get_contents() return to my php script in the variable $contents?

Would $contents be empty? Or would $contents be some form of error? (for example) such as

enter image description here

when viewing in chrome? (hypothetically)

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
Justice
  • 169
  • 1
  • 2
  • 9

1 Answers1

2

It should return FALSE.

See here: file_get_contents

'On failure, file_get_contents() will return FALSE.'

You can always test it by entering a fake URL, i.e a URL that cannot be reached.

You can check the response code (to see if it's a 404) by using:

file_get_contents("http://example.com");
var_dump($http_response_header);

This is taken from the answer to this question: HTTP requests with file_get_contents, getting the response code

http://php.net/manual/en/reserved.variables.httpresponseheader.php

Community
  • 1
  • 1
jd182
  • 3,180
  • 6
  • 21
  • 30
  • 1
    But note, that calling a *invalid* url on a *valid* server adress will most likely redirect you to the servers 404 page, which will then be downloaded as a string. – dognose Jan 26 '14 at 15:23
  • Is there a way to detect if the server's 404 page are the contents downloaded or not? @dognose – Justice Jan 26 '14 at 15:26
  • The best way would be to check the response code, see updated answer. – jd182 Jan 26 '14 at 15:29