3

I have 2 files (data1.php & data2.php) which fetch data through CURL. I have included these files in another page like index.php. One of these files (data1.php) sometimes takes too much time and hangs the page. I would like to know if there is a way which can check the load time of data1.php. If load time exceeds say 8 seconds, then it stops execution of data1.php and loads data2.php instead.

The data1.php often hangs, but is more relevant to the website, so it needs to be there. But when it hangs I want the page to display something closely relevant so data2.php. I tried calculating start & stop time with microtime() but since the page hangs, it does not solve the issue.

$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$start = $time;

How can I calculate loading time of data1.php amd if it takes more time then automatically stop its execution and load data2.php instead?

halfer
  • 19,824
  • 17
  • 99
  • 186
user2649343
  • 147
  • 3
  • 11
  • What is the difference between data1 and data2. If the second one loads quicker, then why do you need the first one? – Nigel Ren Sep 14 '19 at 11:53
  • 1
    You should add a timeout parameter in your cURL call - `CURLOPT_TIMEOUT` in seconds. When it timeouts - the result code will be non-zero, you can detect this and call the less relevant alternative. – IVO GELOV Sep 14 '19 at 12:00
  • Thanks @IVOGELOV your suggestion worked. Had to research a bit more, but thanks for the correct direction. – user2649343 Sep 14 '19 at 15:05

0 Answers0