我不知道是否有可能有一个if语句是这样的:PHP的file_get_contents返回错误
if file_get_contents() returns error code xxx OR xxx
die();
我不知道如何检查file_get_contents()
返回一个错误。
我不知道是否有可能有一个if语句是这样的:PHP的file_get_contents返回错误
if file_get_contents() returns error code xxx OR xxx
die();
我不知道如何检查file_get_contents()
返回一个错误。
http://php.net/file_get_contents
该函数返回的读取数据或FALSE的失败。
它要么工作,要么不,因为这样的:
$file = file_get_contents(...);
if ($file === false) {
die();
}
PS:当然,在上面的答案我希望你用file_get_contents
获得一个本地文件的内容。如果您试图获取远程URL的HTTP状态码,我建议您使用cURL或类似的专用HTTP库并解析响应头。
随着php docs状态,file_get_contents()
将所有错误返回false
,因此,所有你能做的就是
if ($content=file_get_contents($filename)===false) die("Error reading file $filename");
似乎你希望得到的HTTP状态代码。在这种情况下,你需要从这里阅读:
$http_response_header
http://php.net/manual/en/reserved.variables.httpresponseheader.php
否则,如果你想要的一般性错误,请按照deceze的指示。
,你可以调用它的方法:
$data = file_get_contents("http://site.com/that.php");
if ($data !== FALSE) {
// analyze data
}
嗯,我试图让远程JSON文件,但我相信上面的代码仍然可以工作,我并不需要一个特定代码,就像你说的那样,它可以工作,也可以不工作。 – Archey