0

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.

Edited by RikTelner

2
Contributors
2
Replies
20
Views
1 Year
Discussion Span
Last Post by RikTelner
0

Something is actually wrong . The API that you are using . (if you use it in correct way and these are the responses)

0

Something is actually wrong . The API that you are using .

Not really. Even if you use raw address in the browser or cURL it sometimes doesn't reply well.

Maybe confusion came from the fact that API is hosted elsewhere and when I refer to server, I mean my host, not host of the API.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.