Page 1 of 1

Need PHP help. Timeout problems during execution??

Posted: Thu Dec 18, 2003 12:42 pm
by Grant29
Hi,

I'm working on some code to "scrape" the rebate amount off an Amazon page. So far my code works, but it seems to abort before finishing. I'm not sure if it's a problem in my code, or a timeout setting in php or apache. I'm running php v4.3.2 and Apache 2.0.48 on Windows.

The code seems to stop in slightly different places each time. I can't figure it out though. Please help! I know that the test conditions are redundant, it's just there for test purposes.

Here is my code: (You should be able to cut and paste it to run it.)

Code: Select all

<?

function get_rebate_amt($asin)
&#123;
  echo "ASIN: $asin <br>";
  echo "URL:
http://www.amazon.com/exec/obidos/tg/detail/-/$asin/
<br>";
  // Get any info on rebates
  $rebate_amt = "N/A";
 
  $f =
fopen("http://www.amazon.com/exec/obidos/tg/detail/-/$asin/",
'r');
  echo "f: $f <br>";
  if($f)
    &#123;
      //echo "here1 <br>";
      $page = "";	  
      while($pre = fread($f,100000))
	&#123;
	  $page = $page.$pre;
	&#125;
      //echo "here2 <br>";
      // Check to see if a rebate is availiable
      $rebate_avail = substr_count($page, "Mail-In
Rebates");
      if ($rebate_avail) &#123;
	//echo "here3 <br>";
	// Cut out most usless junk
	$temp = ereg("Mail-In Rebates(.*)Price After
Rebates", $page,
$content);
	$temp = ereg_replace("<br>", "<br>", $content&#1111;1]);
	$temp = ereg("price>(.*)</span>", $temp, $content);
	$rebate_amt = ereg_replace("<br>", "<br>",
$content&#1111;1]);
      &#125;
    &#125;
  else &#123;
    $rebate_amt = "N/A";
  &#125;
  //echo "func: Rebate amount: $rebate_amt <br>";
  unset($rebate_avail);
  unset($temp);
  unset($pre);
  unset($page);
  unset($f);
  fclose($f);
  return $rebate_amt;
&#125;


$cnt = 0;
while ($cnt < 40) &#123;
  echo "cnt: $cnt <br>";
  echo date('h:i:s');
  echo "<br>";
  if      ($cnt == 0) $asin = "B00009J5VX";
  else if ($cnt == 1) $asin = "B00005AC8J";
  else if ($cnt == 2) $asin = "B0000DB4D1";
  else if ($cnt == 3) $asin = "B00005U0HU";
  else if ($cnt == 4) $asin = "B00006RHOD";
  else if ($cnt == 5) $asin = "B00009J5W0";
  else if ($cnt == 6) $asin = "B00007KDVI";
  else if ($cnt == 7) $asin = "B00006LHJY";
  else if ($cnt == 8) $asin = "B00007K3A3";
  else if ($cnt == 9) $asin = "B00006RTY7";

  else if ($cnt == 10) $asin = "B00009J5VX";
  else if ($cnt == 11) $asin = "B00005AC8J";
  else if ($cnt == 12) $asin = "B0000DB4D1";
  else if ($cnt == 13) $asin = "B00005U0HU";
  else if ($cnt == 14) $asin = "B00006RHOD";
  else if ($cnt == 15) $asin = "B00009J5W0";
  else if ($cnt == 16) $asin = "B00007KDVI";
  else if ($cnt == 17) $asin = "B00006LHJY";
  else if ($cnt == 18) $asin = "B00007K3A3";
  else if ($cnt == 19) $asin = "B00006RTY7";

  else if ($cnt == 20) $asin = "B00009J5VX";
  else if ($cnt == 21) $asin = "B00005AC8J";
  else if ($cnt == 22) $asin = "B0000DB4D1";
  else if ($cnt == 23) $asin = "B00005U0HU";
  else if ($cnt == 24) $asin = "B00006RHOD";
  else if ($cnt == 25) $asin = "B00009J5W0";
  else if ($cnt == 26) $asin = "B00007KDVI";
  else if ($cnt == 27) $asin = "B00006LHJY";
  else if ($cnt == 28) $asin = "B00007K3A3";
  else if ($cnt == 29) $asin = "B00006RTY7";

  else if ($cnt == 30) $asin = "B00009J5VX";
  else if ($cnt == 31) $asin = "B00005AC8J";
  else if ($cnt == 32) $asin = "B0000DB4D1";
  else if ($cnt == 33) $asin = "B00005U0HU";
  else if ($cnt == 34) $asin = "B00006RHOD";
  else if ($cnt == 35) $asin = "B00009J5W0";
  else if ($cnt == 36) $asin = "B00007KDVI";
  else if ($cnt == 37) $asin = "B00006LHJY";
  else if ($cnt == 38) $asin = "B00007K3A3";
  else if ($cnt == 39) $asin = "B00006RTY7";
  
  $rebate_amt = get_rebate_amt($asin);
  echo "Rebate Amount: $rebate_amt<br>";
  echo "<hr>";
  $cnt++;
&#125;


?>

Thanks,
Grant

Posted: Thu Dec 18, 2003 1:52 pm
by Weirdan
try to call [php_man]set_time_out[/php_man] before the call of get_rebate_amt.