for*_*old 2 php debugging error-handling file file-get-contents
我尝试这样做:
$urls = array(
"https://www.gov.sg/"
);
foreach ($urls as $url) {
d($url);
// $url = "http://www.google.com";
$data = file_get_contents($url);
d($data);
}
Run Code Online (Sandbox Code Playgroud)
d() 是像 var_dump() 一样的第三方函数。
我不断得到 $data = false。似乎域中的任何页面都在这样做。
当我尝试另一个域(如 google.com)时,它奏效了。
该页面有效,我在浏览器上尝试过。
以下类似的帖子提出了 URL 编码问题,并启用了 allow_url_fopen,但这些在这里并不真正适用: PHP file_get_contents 返回 false PHP file_get_contents 返回 false
为什么函数返回false?另外,一般来说,有没有办法检查为什么 file_get_contents 返回 false?PHP 有没有办法转储错误?
你可以得到这样的最后一个错误,
$error = error_get_last();
var_dump($error);
Run Code Online (Sandbox Code Playgroud)
例如,test.php
<?php
$data = file_get_contents("http://hi200.com");
var_dump($data);
$error = error_get_last();
var_dump($error);
Run Code Online (Sandbox Code Playgroud)
php test.php
PHP Warning: file_get_contents(http://hi200.com): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden
in C:\Users\Dell\Desktop\HQ_Educations\library\test.php on line 3
bool(false)
array(4) {
["type"]=>
int(2)
["message"]=>
string(105) "file_get_contents(http://hi200.com): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden
"
["file"]=>
string(52) "C:\Users\Dell\Desktop\HQ_Educations\library\test.php"
["line"]=>
int(3)
}
Run Code Online (Sandbox Code Playgroud)