views:

345

answers:

4

Hi,

I'm including a local class that requests a file from a remote server. This process however, is rather unreliable (in the sense that the remote server is often overloaded), and I would have to wait a few seconds (about 20 or so) before the include gives up and continues. Now, I would like to have a time limit on the execution time of the included script, say 5 seconds.

Current code:

include('siteclass.class.php');

Thx for the help!

//update thx ppl:

My code inside the class: `$movie = str_replace(" ","+",$movie); $string = join('',file($siteurl.$l.'/moviename-'.$movie));

if(!$i) { static $i = 1;} if($file_array = $string) {

$result = Return_Substrings($file_array, '<item>', '</item>');

  foreach($result as $res) {`

That's basically it, as far as the loading goes. The internal processing takes about 0.1 s. I guess that's pretty doable.

thx again!

A: 

This isn't an exact fit to what you're looking for, but this will set the time limit for the include and execution to a total of 25 seconds. If the time limit is reached, it throws a fatal error.

set_time_limit(25);
James Skidmore
A: 

It sounds like set_time_limit() might do what you want:

PHP Manual for that function

inkedmn
A: 

Fix the included code to have a timeout on the HTTP Request and then recover nicely, instead of just aborting by setting a time limit on the script itself.

My advice would be to get to the root of the problem instead of looking for a workaround.

Kekoa
what do you recommend with my (newly updated) code? i do agree with your vision > fix it at the root. thx
Maurice Kroon
A: 

Note that I didn't test this code, take this like a proposition :

$fp = fopen('siteclass.class.php', 'r');

stream_set_timeout($fp, 2);
stream_set_timeout($fp,$timeout);
$info = stream_get_meta_data($fp);

if ($info['timed_out']) {
  echo "Connection Timed Out!";
} else {
  $file = '';
  while (!feof($fp)) {
    $file .= fgets($fp);
  }
  eval($file);
}

The timeout is set in seconds, so the example set it to two seconds.

Sylvain
thx for the help. it turns out that my php file returns blank. No errors nothing, just blank. I guess it has something to do that i'm not including the class inside the page. (not sure though>not an expert), but i'm missing some variables requested in the class. (some global variables set, such as '$movie'. What do you think? Thx anyway!
Maurice Kroon
ok, i apologize for my incompetence.. But, it turned out i forgot to strip the '<?php' from the class files.anyway, it works now! thx!!
Maurice Kroon