PDA

View Full Version : PHP Hammering


lavinpj1
05-27-2006, 01:31 AM
I have made the following to put the last 5 news entries on a site's RSS feed into a database...


<?php
$content = file_get_contents("http://flyinghamster.com/feed/");

$pattern = '@(<title>).*?(?=</title>)@';
preg_match_all($pattern, $content, $title);

$pattern = '@(<link>).*?(?=</link>)@';
preg_match_all($pattern, $content, $link);

$pattern = '@(<pubDate>).*?(?=\+[0-9]{4}</pubDate>)@';
preg_match_all($pattern, $content, $date);

mysql_connect('localhost', 'xxx', 'xxx');
mysql_select_db('xxx');

$pattern = '@<[^<>]+>@';

for ($i = 1; $i <= 5; $i++) {
$tit = preg_replace($pattern, '', $title[0][$i]);
$dat = preg_replace($pattern, '', $date[0][$i]);
$lin = preg_replace($pattern, '', $link[0][$i]);
mysql_query("INSERT INTO `hamster` SET `Title`='$tit', `Date`='$dat', `Link`='$lin'");
}
unset($content);
unset($tit);
unset($dat);
unset($lin);
unset($title);
unset($link);
unset($date);
mysql_close();
?>


This is run on a cron job every 1 min and I loaded it a few hours ago. I then tried to convert some pictures to thumbnails on my gallery, a task which php normally manages just fine with. This returned an out of memory error. I had to raise PHPs memory limit to 32mb before it would work.

Anyone have any suggestions as to why this could be, and how to prevent it?

~Phil~

JamieD
05-27-2006, 01:38 AM
running the script every minute it rather excessive. Looking at the times of the postings on the feed you are using articles are published every hour. Try running your script every hour at 5 or 10 minutes past the hour. This should solve your problem and your news will still be up to date.

lavinpj1
05-27-2006, 01:47 AM
Hrmmm, very true. But does that not mean I will have exactly the same problem, just 10x slower?

~Phil~