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.