There is server A on which the user loads the page. When generating this page, the server must request some data from server B, which can respond quickly (<0.5 sec), long (0.5 to 5 sec), not respond at all (> 5 sec). In the second case, you need to give the user a page, but wait for the answer and save it to a file, in the third case, just give the user a page so that it is saved locally. How to implement it correctly?

The best thing that comes to mind is to poke server B through @file_get_contents, if something responds — save to a file, if it doesn't answer — take it from a file that was saved during the last successful response. But in this case, the page will not load until server A fulfills the entire request to B, which means that in the second situation we will smoke up to 5 seconds.

    1 answer 1

    Download asynchronously server A from server B. For example, you call a command to get a file in the background. Or use the task queue, which is a radish type thread. The general scheme will be

    Clinet-> Server A (responds to the client with a page on which the text is written, stub and revokes the AJAX with a periodic request to server A (script B) - is there anything to show and request (background) on the server (background server) script (C))

    Script B - checks the status - if there is a file (for example, through the database), if there is - gives it, if not, gives it - wait another.

    The script C is launched by the background on the server, sets the flag that we load, loads the file from server B, and upon completion sets the flag - that everything is fine.

    The scheme is approximately the same. Periodic request AYAX can be converted to nodejs for example, but then you can do the whole project on it :-)

    Running as a background is either exec if I'm not mistaken, or you can set the cron to listen.