I tried doing this:
$urls = array(
"https://www.gov.sg/"
);
foreach ($urls as $url) {
d($url);
// $url = "http://www.google.com";
$data = file_get_contents($url);
d($data);
}
d() is a 3rd-party function like var_dump().
I keep on getting $data = false. It seems like any page from the domain is doing that.
When I tried another domain like google.com, it worked.
The page is valid, I tried it on a browser.
The following similar posts suggest URL encoding issues, and enabling allow_url_fopen, but these don't really apply here: PHP file_get_contents returning false PHP file_get_contents returning false
Why is the function returning false? Also, in general, is there a way to check why a file_get_contents is returning false? Is there a way for PHP to dump the error?
You can get last error like this,
$error = error_get_last();
var_dump($error);
for example, test.php
<?php
$data = file_get_contents("http://hi200.com");
var_dump($data);
$error = error_get_last();
var_dump($error);
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)
}