Jump to content


PHP - Rss to MySql


2 replies to this topic

#1 Corey

    Young Padawan

  • Members
  • Pip
  • 12 posts
  • Gender:Male
  • Location:Canada

Posted 10 May 2007 - 03:04 PM

Hello; I am currently trying to construct a 'rss bot' which would check for updated news via a rss channel. It would need to check first to make sure the same article hasn't been posted, if it is, not post anything, if the article hasn't been posted, it would post the title, description, and link. It would be doing this from a mysql database.

I have a script which will grab the info (title, description, and link..)
<?php
$site = "http://newsrss.bbc.co.uk/rss/newsonline_world_edition/front_page/rss.xml";
 

$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
function startElement($parser, $name, $attrs) {
global $insideitem, $tag, $title, $description, $link;
if ($insideitem) {
$tag = $name;
} elseif ($name == "ITEM") {
$insideitem = true;
}
}
function endElement($parser, $name) {
global $insideitem, $tag, $title, $description, $link;
if ($name == "ITEM") {
//echo's the content

//$insert = mysql_query("INSERT INTO `newstest` (`cid`,`title`,`date`,`author`,`story`,`ip`,`from`) VALUES ('1','$title','$date','RSS Bot','$description $link','NO IP','BBC')");

printf("<dt><u><a href='%s'>%s</a></u></dt>",
trim($link),trim($title));
printf("<dd><i>%s</i></dd><br><br>",trim($description));
$title = "";
$description = "";
$link = "";
$insideitem = false;
}
}
function characterData($parser, $data) {
global $insideitem, $tag, $title, $description, $link;
if ($insideitem) {
switch ($tag) {
case "TITLE":
$title .= $data;
break;
case "DESCRIPTION":
$description .= $data;
break;
case "LINK":
$link .= $data;
break;
}
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen("$site","r")
or die("Error reading RSS data.");
while ($data = fread($fp, 4096))
xml_parse($xml_parser, $data, feof($fp))
or die(sprintf("XML error: %s at line %d", 
xml_error_string(xml_get_error_code($xml_parser)), 
xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);
?>

You can see I commented the mysql query to insert it into the database. When uncommented, it posts it, but posts all the articles in the feed. I need it to first check if the article has been posted. Then if it hasn't, post it, and if it already has been posted, don't do anything, and move on to see if the other news has been posted.

Thanks to anyone who can help with figuring this out.

Edited by Corey, 10 May 2007 - 03:05 PM.


#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 10 May 2007 - 04:27 PM

If you're going to be reading and parsing the RSS anyway, i would recommend not wasting time by storing it in the database. That way, who's ever content you're ripping will update automatically when they update it, and you don't waste space storing it.

But to compare posts, you'd first have to parse the XML, then select any matching posts from your database and compare the results... another waste of time.

Edited by rc69, 10 May 2007 - 04:28 PM.


#3 Corey

    Young Padawan

  • Members
  • Pip
  • 12 posts
  • Gender:Male
  • Location:Canada

Posted 10 May 2007 - 05:41 PM

Thanks for explaining it in words, but I already knew everything you said. I just don't know how to do it and that is what I am asking help with; hence, thats why I posted here.

Edited by Corey, 10 May 2007 - 05:41 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users