Jump to content

Recommended Posts

hi, im using this script that somebody else has made. well some feeds work just fine, some feeds end up with nothing in the item array at all and some feeds come up with this fopen error.

 

**EDIT** nevermind on the fopen problem. i cant believe i didnt catch that. feed://http//www.standardbyke.com/blog/?feed=rss2 haha

**RE-EDIT** actually, it would still be nice to know how to make my own error message instead of just killing the entire script because of an fopen error. thanks guys.

 

 

but i would still like some help with this if possible.

 

this is a feed that makes it through fopen and all the checks but comes out with nothing in the array.

http://brickhousebikes.blogspot.com/feeds/posts/default

 

here is the script

 

class RDFParser
{
//
// variables
//
// set up local variables for this class
var $currentTag = "";
var $flag = "";
var $count = 0;
// this is an associative array of channel data with keys ("title", "link", "description")
var $channel = array();
// this is an array of arrays, with each array element representing an <item>
// each outer array element is itself an associative array 
// with keys ("title", "link", "description")
var $items = array();
//
// methods
//
// set the name of the RDF file to parse
// this is usually a local file
// you may set it to a remote file if your PHP build supports URL fopen()
function setResource($file)
{
$this->file = $file;
}
// parse the RDF file set with setResource()
// this populates the $channel and $items arrays
function parseResource()
{
// create parser
$this->xp = xml_parser_create();
// set object reference
xml_set_object($this->xp, $this);
// set handlers and parser options
xml_set_element_handler($this->xp, "elementBegin", "elementEnd");
xml_set_character_data_handler($this->xp, "characterData");
xml_parser_set_option($this->xp, XML_OPTION_CASE_FOLDING, TRUE);
xml_parser_set_option($this->xp, XML_OPTION_SKIP_WHITE, TRUE);
// read XML file


if (!($fp = fopen($this->file, "r"))) 
{
die("Could not read $this->file");
}



// parse data
while ($xml = fread($fp, 4096)) 
{


if (!xml_parse($this->xp, $xml, feof($fp))) 
{
die("XML parser error: " .
xml_error_string(xml_get_error_code($this->xp)));
}


}
// destroy parser
xml_parser_free($this->xp);
}
// opening tag handler
function elementBegin($parser, $name, $attributes)
{
$this->currentTag = $name;
// set flag if entering <channel> or <item> block
if ($name == "ITEM")
{
$this->flag = 1;
}
else if ($name == "CHANNEL")
{
$this->flag = 2;
}
}
// closing tag handler       
function elementEnd($parser, $name)
{
$this->currentTag = "";
// set flag if exiting <channel> or <item> block
if ($name == "ITEM")
{
$this->count++;
$this->flag = 0;
}
else if ($name == "CHANNEL")
{
$this->flag = 0;
}
}
// character data handler
function characterData($parser, $data)
{
$data = trim(htmlspecialchars($data));
if ($this->currentTag == "TITLE" || $this->currentTag == "LINK" || $this->currentTag == "DESCRIPTION")
{
// add data to $channels[] or $items[] array
if ($this->flag == 1)
{
$this->items[$this->count][strtolower($this->currentTag)] .= $data;
}
else if ($this->flag == 2)
{
$this->channel[strtolower($this->currentTag)] .= $data;
}
}
} 
// return an associative array containing channel information
// (the $channel[] array)
function getChannelInfo()
{
return $this->channel;
}
// return an associative array of arrays containing item information
// (the $items[] array)
function getItems()
{
return $this->items;
}
}

 

and here is what im using to run it

 

 // get and parse 
$f = new RDFParser();
$f->setResource("$RssFile");
$f->parseResource();
$f_channel = $f->getChannelInfo();
$f_items = $f->getItems();
// now format and print it...

$NewsBlock = "<div class=\"RssFeed\"><h1><a href=\"$f_channel[link]\">$f_channel[title]</a></h1><ul>";
// iterate through items array
for ($x=0; $x<$MaxItems; $x++) {
  if (is_array($f_items[$x])) {

   // print data
   $item = $f_items[$x];
   $NewsBlock .= "<li><a href=\"$item[link]\" title=\"$item[title]\">$item[title]</a></li>";
  }
}
$NewsBlock .= "</ul></div>";

 

any help with this would be awesome. if nothing else, it would be nice to not have the script just die when fopen fails and be able to put in my own error message instead. thanks.

Link to comment
https://forums.phpfreaks.com/topic/67989-help-with-rss-parser/
Share on other sites

just throw @ in front of the function? ill give that a try.

 

what else would you like to know?

 

i put in this if statement so at least its not outputting nothing when the array is empty. but i dont know why this is happening int the first place.

 

if($f_items) {
$NewsBlock = "<div class=\"RssFeed\"><h1><a href=\"$f_channel[link]\">$f_channel[title]</a></h1><ul>";
// iterate through items array
for ($x=0; $x<$MaxItems; $x++) {
  if (is_array($f_items[$x])) {

   // print data
   $item = $f_items[$x];
   //$DescID = preg_replace('/[^a-z]/i','',$item[title]);
   $NewsBlock .= "<li><a href=\"$item[link]\" title=\"$item[title]\">$item[title]</a></li>";
  }
}
$NewsBlock .= "</ul></div>";
} else {
  $NewsBlock = "<div class=\"RssFeed\"><h1>Error</h1><ul><li>There was an error retrieving this feed.<br />$RssFile</li></ul></div>";
}

 

thanks for the help. let me know what more information you need and ill see what i can  do. thanks.

Link to comment
https://forums.phpfreaks.com/topic/67989-help-with-rss-parser/#findComment-341840
Share on other sites

cool the @ sign worked for that. so if anybody can figure out why some feeds dont throw up errors but dont seem to have anything extracted from them either i will stop bothering you :).

 

could it be because they are "atom"? im not sure what difference that makes. or id they are atom or not. thanks..

Link to comment
https://forums.phpfreaks.com/topic/67989-help-with-rss-parser/#findComment-341843
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.