Jump to content

rss links


rarebit

Recommended Posts

Hi

I wrote an rss parser thing the other week and have just got around to trying to do something with it when I noticed that the links wernt being made. On investigation it seems that the associative elements name isn't 'link', but 'id'. (http://uk3.php.net/feed.atom)

 

Is this usual, or should I just check for both?

If so are there anything else I should be seeking out?

Link to comment
https://forums.phpfreaks.com/topic/126455-rss-links/
Share on other sites

well i'm trying to handle both in one...

this is what i've just done to part of the output bit...

$link = NULL;
if(array_key_exists('link', $e))
{
$link = $e->link['href'];
}
elseif(array_key_exists('id', $e))
{
$link = $e->id;
}

 

it seems to be working on the two examples i'm currently using...

 

cheers

Link to comment
https://forums.phpfreaks.com/topic/126455-rss-links/#findComment-653863
Share on other sites

probably true!

 

also the above was somewhat out, now changed to this:

if(array_key_exists('link', $e))
{
if(isset($e->link['href']))
{
	$link = $e->link['href'];
}
else
{
	$link = $e->link;
}
}
elseif(array_key_exists('id', $e))
{
$link = $e->id;
}

 

not actually sure if the last section is required?

 

 

 

This is how i'm distinguishing them apart at the moment:

function rss_do_rss($xml)
{
$sret = "<table width='300px' border='1px' cellspacing='0px' cellpadding='3px'>";
if(strcmp($xml->attributes()->version, "2.0")==0)
{
	$x = $xml->channel;
	$sret .= rss_xml_print_e($x, 1);
	foreach ($x->item as $e)
	{
		$sret .= rss_xml_print_e($e);
	}
}
else
{
	$n = 1;
	foreach($xml as $k => $v)
	{
		$sret .= rss_xml_print_e($v, $n);
		if($n==1)
		{
			$n = 0;
		}
	}
}
$sret .= "</table>";
return $sret;
}

 

Is this adequate?

 

 

p.s. $xml already contains the feed depending upon whether it was from cache or a fetched from a link.

Link to comment
https://forums.phpfreaks.com/topic/126455-rss-links/#findComment-653877
Share on other sites

gotcha ;) I suppose that is a way to distinguish between them though I'm not sure why you used strcmp. There is often more than 1 way to do things, some may use headers, some may use the content of the file. Advantages to using headers would be that you can tell the type from the very beginning and will be able to tell the type even if the content is corrupted.. disadvantage would be if the server fails to send the proper header for some reason you wouldn't know how to parse.

Link to comment
https://forums.phpfreaks.com/topic/126455-rss-links/#findComment-653912
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.