Hello All,
I cannot seem to get this to work. The rss feed gets loded to a temp file on the server, but the page does not display the information. Can anyone help? Thanks in advance. This is based on craigslist.org rss listing for furniture.
http://newyork.craigslist.org/fur/index.rss
This is the phpFruction code
<?PHP
$thisitem = "";
$isitem = false;
$aDisplay = array();
$counter = 0;
$numRows = 5;
/
Global variables
The element start function is used by the main function when an opening taf is found.
Pass it the $xmlparser object as well as the tag.
/
function elementStart($xmlparser,$tag){
global $thisitem, $isitem;
/
If the valus of $tag is ITEM then we set $isitem to true
/
if($tag == "item"){
$isitem=true;
}
$thisitem = $tag;
}
/
This function is run when a closing tag is found. if it
finds item then $isitem is set to false
/
function elementEnd($xmlparser, $tag){
global $isitem, $counter;
if($tag == "item"){
$isitem=false;
$counter++;
}
}
/
The datahandler function deals with the data within the <item> </item> tags.
We check to see if we are up to our limit of items that we want to return.
If we are not we check that we have an item and add it to our array of $aDisplay
items.
/
function dataHandler($xmlparser, $data){
global $aDisplay, $counter, $thisitem, $isitem;
$numRows;
if($counter < $numRows){
if($isitem){
$tag = strtolower($thisitem);
$aDisplay[$counter]["tag"].=$data;
}
}
}
/*
Accessing the Feed
*/
function getFeed($feed,$feedname,$no){
global $aDisplay, $numRows;
/
Sections of Data Returned
/
$numRows = $no;
$tmp = $feedname."_tmp.rdf";
$expires=time()-3600;
if(!file_exists($tmp) or
filemtime($tmp) < $expires){
@copy($feed, $tmp) or die("File copy to temp file failed");
}
/*
Read XML Data
*/
$xml = "";
$filehandle = @fopen($tmp, "r") or die("Cannot open temporary file");
while (!feof($filehandle)){
$xml.= fread($filehandle, 4096);
}
fclose($filehandle);
/*
Getting the work done.
*/
$xmlparser = xml_parser_create();
@xml_set_element_handler($xmlparser, "elementStart", "elementEnd");
@xml_set_character_data_handler($xmlparser, "dataHandler");
@xml_parse($xmlparser, $xml) or die("Error parsing data file");
xml_parser_free($xmlparser);
return $aDisplay;
}
?>
This is the code for the page displaying the RSS feed.
<?php
include("rss_functions.php"); ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Test XML Feed to a webpage</title>
<link href="stylescreen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<p><a href="http://www.solieworks.com">Furniture on Craigslist</a></p>
<p> </p>
<ul>
<?php
// Retrieve the info
$a = getFeed("http://newyork.craigslist.org/fur/index.rss","NewYorkFurniture",10);
if (is_array($a)){
foreach($a as $row){?>
<li><a href="<?php echo "$row['link']"; ?>"><?php echo $row['title']; ?></a></li>
<?php
}
}
?>
</ul>
<p> </p>
</body>
</html>