php include only if HTTP request succeeds

Viewed 30

Hi,

I have this code:

 ob_start();
 include( "https://example.net/news.php" );
 $data = ob_get_contents();
 ob_end_clean(); 

problem is that sometimes the request seems to fail for some reason (despite the requested file and the file that makes the request are on the same server) and then the page wont finish loading which is putting a lot of load on the server. This is the error I get:

PHP Warning:  include(): Failed opening 'https://example.net/news.php' for inclusion

So I was thinking to condition the include only if the request succeeds so I wont have this problem. How can I do that?

Thank you.

0 Answers
Related