Вопрос

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)

Это было полезно?

Решение

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top