Score:0

PHP file_get_contents sometimes returns 502 on 1 of 2 Servers

de flag

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

Score:0
de flag

I suspect your new server is hitting some sporadic network timeout (DNS?). To dig deeper, try to log a detailed error right after failing file_get_contents() and duration of the request before the failure:

$t0 = microtime(true);
if (($data = @file_get_contents("http://mirror.facebook.net/centos/timestamp.txt")) === false) {
    $t1 = microtime(true);
    var_dump($t1 - $t0);  // duration
    var_dump(error_get_last());
}

These outputs combined might give you hints about further steps.

de flag
Thanks for the idea. Will implement see and post what result it will provide
de flag
As mentioned above I've added my results. Still not more details. Any other suggestions?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.