rarebit Posted September 30, 2008 Share Posted September 30, 2008 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 More sharing options...
genericnumber1 Posted September 30, 2008 Share Posted September 30, 2008 that's an atom feed, not an rss feed. Link to comment https://forums.phpfreaks.com/topic/126455-rss-links/#findComment-653856 Share on other sites More sharing options...
rarebit Posted September 30, 2008 Author Share Posted September 30, 2008 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 More sharing options...
genericnumber1 Posted September 30, 2008 Share Posted September 30, 2008 I would separate the two specifications into two different grammars... they could both use the same parser, but just are looking for different things. You could easily decide which grammar to use based upon the header. Link to comment https://forums.phpfreaks.com/topic/126455-rss-links/#findComment-653870 Share on other sites More sharing options...
rarebit Posted September 30, 2008 Author Share Posted September 30, 2008 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 More sharing options...
genericnumber1 Posted September 30, 2008 Share Posted September 30, 2008 I can't really read your code without seeing all of it... which I don't have time to go through Link to comment https://forums.phpfreaks.com/topic/126455-rss-links/#findComment-653886 Share on other sites More sharing options...
rarebit Posted September 30, 2008 Author Share Posted September 30, 2008 if(strcmp($xml->attributes()->version, "2.0")==0) { ... } else { ... } Link to comment https://forums.phpfreaks.com/topic/126455-rss-links/#findComment-653901 Share on other sites More sharing options...
genericnumber1 Posted September 30, 2008 Share Posted September 30, 2008 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.