So I decided to play around with PHP and XML. My objective was to create a site which will retrieve job postings from a RSS feed. I am currently tweaking it but so far it is working great. Here are some pointers for any souls trying to to do something similar.
~david macias
I retrieved this tutorial from this Great Site.
I have updated this article with this new story.Please take a look at the comments made in the source code. This code will retrieve a feed, parse it, then print only the parts I really care about. For the most part the code is self explanatory, but I have included some comments to help out the newbies like me.
<html>
<head>
<title>DAM's RSS/XML and PHP</title>
</head>
<body>
<h2>Currently...</h2>
<dl>
<?php
class RSSParser {
var $insideitem = false; //Check out the what is inside the <title> tags, and these variables should reflect what you want to pull out
var $sectitle = "";
var $title = "";
var $link = "";
var $creator = "";
var $language = "";
var $source = "";
var $rights = "";
var $date = "";
var $type = "";
//This function checks if you are inside a new item
function startElement($parser, $tagName, $attrs) {
if ($this->insideitem) {
$this->tag = $tagName;
}
elseif ($tagName == "ITEM") {
$this->insideitem = true;
}
}
//functions prints out the data retreived from the RSS feed
function endElement($parser, $tagName) {
if ($tagName == "ITEM") {
printf("<dt><b><a href='%s'>%s</a></b> [ %s ]</dt>", trim($this->link),htmlspecialchars(trim($this->title)),htmlspecialchars(trim($this->source)));
printf("Date: %s <BR>",htmlspecialchars(trim($this->date)));
printf("Creator: %s<BR>",htmlspecialchars(trim($this->creator)));
printf("Rights: %s<BR><BR>",htmlspecialchars(trim($this->rights)));
$this->title = "";
$this->link = "";
$this->source = "";
$this->date = "";
$this->rights = "";
$this->creator = "";
$this->insideitem = false;
}
}
function characterData($parser, $data) {
if ($this->insideitem) {
switch ($this->tag) {
case "DC:TITLE":
$this->title .= $data;
break;
case "LINK":
$this->link .= $data;
break;
case "DC:CREATOR":
$this->creator .= $data;
break;
case "DC:RIGHTS":
$this->rights .= $data;
break;
case "DC:DATE":
$this->date .= $data;
break;
case "DC:SOURCE":
$this->source .= $data;
break;
}
}
}
}
$xml_parser = xml_parser_create();
$rss_parser = new RSSParser();
xml_set_object($xml_parser,&$rss_parser);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$xml_parser = xml_parser_create();
$rss_parser = new RSSParser();
xml_set_object($xml_parser,&$rss_parser);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp1 = fopen("htt://YourSiteFeedGoesHere/rss.xml","r")
or die("Error reading RSS data.");
while ($data = fread($fp1, 4096))
xml_parse($xml_parser, $data, feof($fp1))
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($fp1);
xml_parser_free($xml_parser);
?>
</dl>
</body>
</html>