Currently I have something like this (a bit modified):
function function1($param) {
$ans = @file_get_contents($param);
if (!$ans) return ("Failure");
return json_decode($ans, true);
}
Extremely straight forward. However, the file_get_contents()
gets answer from an API, that has proven to be a little stinky boa. It sometimes answers incorrectly, or "Access Denied" even though it shouldn't. It fails sometimes when it shouldn't, but re-requesting brings the expected result. So I "invented" something like this:
function function1($param) {
$ans = @file_get_contents($param);
if (!$ans) {
usleep(500); // Wait 500ms before re-requesting to prevent spamming queries
$retry++;
if ($retry >= 3) return ("Something is actually wrong this time");
return function1($param);
}
$retry = 0;
return json_decode($ans, true);
}
But it causes Internal Server Error. And it can't be, because as soon as I revert script to original there's no errors, this script executes for MINIMALLY 5 times longer than 3 API calls and 1500ms combined, and after some time none of the elements (not the file_get_contents()
, nor the page, nor the host) respond in time-out. They try to get elements and on fail, instead of returning "Something is actually wrong", it responds with "Internal Server Error" (error message 500 I believe) from the server itself.