I have two virtual servers.
The old: Ubuntu 12.04 with PHP 7.2.
The new: CentOS 7.9 with PHP 8.0.
On both servers the same application is running. A cron is minutely getting some details from another website. similar to the following code
file_get_contents("http://mirror.facebook.net/centos/timestamp.txt")
On the old server, it worked well. I never had issues. On the new server, I have sometimes this Message
Warning: file_get_contents(mirror.facebook.net/centos/timestamp.txt): Failed to open stream: HTTP request failed! HTTP/1.0 502 Bad Gateway
I have no idea why this happens. It affects about 1% of the calls.
Does anyone have an idea where I could look and what I could do to solve the issue?
[EDIT] I implemented the error and time tracking as mentioned below.
First the code:
$t0 = microtime(true);
$jsonString = file_get_contents($pageUrl);
if ($jsonString === false) {
$t1 = microtime(true);
var_dump(sprintf(
'Fehler beim Abruf der URL %s',
$pageUrl,
));
var_dump($t1 - $t0);
var_dump(error_get_last());
return 0;
}
Now the result.
array(4) {
["type"]=>
int(2)
["message"]=>
string(145) "file_get_contents(https://www.example.com/): Failed to open stream: HTTP request failed! HTTP/1.0 502 Bad Gateway
"
["file"]=>
string(62) "/path/to/src/Service/WebcrawlerService.php"
["line"]=>
int(61)
}
The result is sadly NOT providing many more details. It's still 502