I'm just learning PHP and am trying to import rss feeds into a database with this script i found but I keep getting this error
"Fatal error: Call to undefined method RSSDB::clearRssItems() in C:\wamp\www\test\rss2mysql.php on line 263"
Here's the whole script line 263 is near the bottom in green!
I'd really appreciate any help on the error or if there is a better way!
"Fatal error: Call to undefined method RSSDB::clearRssItems() in C:\wamp\www\test\rss2mysql.php on line 263"
Here's the whole script line 263 is near the bottom in green!
Code:
// Settings
//
$file = "http://rss.businessweek.com/bw_rss/classiccars";
$db_hostname = "localhost";
$db_username = "";
$db_password = "";
$db_database = "rss";
/////////////////////////////////////////////////////////////////////////////
error_reporting(E_ALL);
/**
* Database wrapping object for storing RSS items
*/
class RSSDB {
/**
* Set up the database connection.
* @param $db_hostname string with the hostname to which to connect.
* @param $db_username string with the username with which to connect.
* @param $db_password string with the password with which to connect.
* @param $db_database string with the name of the database to use.
*/
function RSSDB($db_hostname, $db_username, $db_password, $db_database) {
$this->db_hostname = $db_hostname;
$this->db_username = $db_username;
$this->db_password = $db_password;
$this->db_database = $db_database;
if (!$this->checkExtension()) {
print("Couldn't load the MySQL connector, check your PHP installation. Aborting.");
exit(-1);
}
if (!$this->getConnection()) {
print("Couldn't connect to the database, aborting.");
exit(-1);
}
}
/**
* Check to see if the MySQL extension is loaded. If not, try to load it.
*/
function checkExtension() {
if (!extension_loaded("mysql")) {
if (!@dl("mysql.so")) {
return(false);
}
}
return(true);
}
/**
* Try to establish a connection to the database server.
* @return true if successful, false otherwise.
* @return Modifies $this->dbConn
*/
function getConnection() {
$this->dbConn = mysql_connect ($this->db_hostname, $this->db_username, $this->db_password);
if (!$this->dbConn) {
return(false);
}
if (!mysql_select_db($this->db_database, $this->dbConn)) {
return(false);
}
return(true);
}
/**
* Perform a query on the database connection
* @param qry String containing the query to execute. Remember to escape properly!
* @return Doesn't return on qry error.
*/
function query($qry) {
if (!mysql_query($qry, $this->dbConn)) {
print("Cannot execute query. Aborting.");
exit(1);
}
}
/**
* Remove all items stored in the database.
*/
function clearRssItems() {
$this->query("DELETE FROM items");
}
/**
* Store a single item in the database.
* @param Lots, see definition. Everything is a string, exceptions:
* @param $dc_date integer representing the date/time as a unix timestamp.
*/
function saveRssItem($title, $link, $description, $dc_creator, $dc_date, $dc_subject) {
$qry = "
INSERT INTO items
(
title,
link,
description,
dc_creator,
dc_date,
dc_subject)
VALUES
(
'".mysql_escape_string($title)."',
'".mysql_escape_string($link)."',
'".mysql_escape_string($description)."',
'".mysql_escape_string($dc_creator)."',
FROM_UNIXTIME(".mysql_escape_string($dc_date)."),
'".mysql_escape_string($dc_subject)."'
);
";
$this->query($qry);
}
}
/**
* Quick'n'Dirty RSS reader and parser. Simply reads in the items in a feed.
*/
class RSSParser {
var $saveItems = array(
"title" => "string",
"link" => "string",
"description" => "string",
"dc:creator" => "string",
"dc:date" => "date",
"dc:subject" => "string",
);
/**
* Read and parse a RSS feed which $url points at (may be an URL or local file)
* @param $url string containing the URL to the RSS feed. May be an URL or local file
*/
function RSSParser($url) {
$this->read($url);
$this->readItems();
}
/**
* Construct a new empty RSS item
* @return a new empty assoc. array representing an rss item
*/
function newItem() {
$retItem = array();
foreach(array_keys($this->saveItems) as $key) {
$retItem[$key] = "";
}
return($retItem);
}
/**
* Read a RSS file/url and make a big string of it so it can be parsed.
* @param $url string containing the URL to the RSS feed. May be an URL or local file
*/
function read($url) {
$this->rssData = implode("", file($url));
}
/**
* Read <item> segments from the RSS file and stuff them in an array.
*/
function readItems() {
if (!isset($this->rssData)) {
return(-1);
}
$this->rssItems = array();
$parser = xml_parser_create();
xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);
xml_parse_into_struct($parser,$this->rssData,$values,$tags);
xml_parser_free($parser);
// Loop through all the elements in the RSS XML file. If an <item> tag
// is found, it's children will be added to a array until the closing
// tag is found. Then the array is added to a list of items
// ($this->rssItems).
for ($i=1; $i < count($values); $i++) {
$tagName = "";
$tagType = "";
$tagValue = "";
if (array_key_exists("tag", $values[$i])) {
$tagName = $values[$i]["tag"];
}
if (array_key_exists("type", $values[$i])) {
$tagType = $values[$i]["type"];
}
if (array_key_exists("value", $values[$i])) {
$tagValue = $values[$i]["value"];
}
if ($values[$i]["tag"] == "item" && $values[$i]["type"] == "open") {
// Looks like we found an <item> tag. Create a new array to
// store it's children values as they will be found on the next
// iteration of the loop.
$rssItem = $this->newItem();
}
if ($values[$i]["tag"] == "item" && $values[$i]["type"] == "close" && isset($rssItem)) {
// </item> tag closed. Store the read item information.
$this->rssItems[] = $rssItem;
unset($rssItem); // No item information will be saved when this doesn't exist.
}
if (array_key_exists($tagName, $this->saveItems) && isset($rssItem)) {
// Found a tag that we want to store and that's part of an
// <item>. Save it.
switch($this->saveItems[$tagName]) {
case "string":
$rssItem[$tagName] = $tagValue;
break;
case "date":
$rssItem[$tagName] = strtotime($tagValue);
break;
default:
print("Don't know how to handle type ".$this->saveItems[$tagName].". Aborting.");
exit(1);
break;
}
}
}
}
}
$db = new RSSDB($db_hostname, $db_username, $db_password, $db_database);
$parser = new RSSParser($file);
[COLOR=Lime]$db->clearRssItems(); // Delete old RSS items.[/COLOR]
foreach($parser->rssItems as $rssItem) {
$db->saveRssItem(
$rssItem["title"],
$rssItem["link"],
$rssItem["description"],
$rssItem["dc:creator"],
$rssItem["dc:date"],
$rssItem["dc:subject"]
);
}
I'd really appreciate any help on the error or if there is a better way!