nickjonnes Posted May 30, 2011 Share Posted May 30, 2011 hey all, i am trying to get the first url from a rss feed this is the rss feed link: 'http://feeds.bbci.co.uk/news/video_and_audio/science_and_environment/rss.xml' i think the issue may be to do with two things first it is a child of the item tag second it has an odd tag name ie. <guid isPermaLink="false"><guid> the feed looks like this <item> <title>this is a title</title> <link>link to the article</link> <guid isPermaLink="false">http://www.cnn.com/video/#/video/bestoftv/2011/05/27/arena.mets.cnn</guid> <description>d</description> <category>bestoftv</category> <pubDate>Sat, 28 May 2011 01:39:51 GMT</pubDate> <feedburner:origLink>http://www.cnn.com/video/#/video/bestoftv/2011/05/27/arena.mets.cnn</feedburner:origLink> </item> my php code to grab the video link <?php if (file_exists('http://feeds.bbci.co.uk/news/video_and_audio/science_and_environment/rss.xml')) { $xmldata = file_get_contents("http://feeds.bbci.co.uk/news/video_and_audio/science_and_environment/rss.xml"); $url = value_in('guid isPermaLink="false', $xmldata); print_r($url); } else { exit('Failed to open xml data'); } ?> i hope someone can help as i have been trying hard to fix this issue cheers nick Quote Link to comment Share on other sites More sharing options...
requinix Posted May 30, 2011 Share Posted May 30, 2011 And what, pray tell, is value_in? Also, are you running PHP 5+? Quote Link to comment Share on other sites More sharing options...
nickjonnes Posted May 30, 2011 Author Share Posted May 30, 2011 oh sorry shouild have said function value_in($element_name, $xml, $content_only = true) { if ($xml == false) { return false; } $found = preg_match('#<'.$element_name.'(?:\s+[^>]+)?>(.*?)'. '</'.$element_name.'>#s', $xml, $matches); if ($found != false) { if ($content_only) { return $matches[1]; //ignore the enclosing tags } else { return $matches[0]; //return the full pattern match } } // No match found: return false. return false; } Quote Link to comment Share on other sites More sharing options...
requinix Posted May 30, 2011 Share Posted May 30, 2011 That function doesn't support tags with attributes. For PHP 4 use DOMDocument; for PHP 5+ use SimpleXML. So which version of PHP do you have? Quote Link to comment Share on other sites More sharing options...
nickjonnes Posted May 30, 2011 Author Share Posted May 30, 2011 php 4. could you please show me how you would do this? cheers nick Quote Link to comment Share on other sites More sharing options...
requinix Posted May 30, 2011 Share Posted May 30, 2011 I'm too lazy to look it up, but the code would look something like this: $dom = new DOMDocument(); $dom->load("url"); $elements = $dom->getElementsByTagName("guid"); foreach ($element in $elements) { echo $element->innerHTML; } The PHP Manual is your friend. Quote Link to comment Share on other sites More sharing options...
nickjonnes Posted May 30, 2011 Author Share Posted May 30, 2011 thanks for that im getting an error saying unexspected '=' sign in line two i remove it and i get a parsing error.? cheers nick btw i got php5 i just stuffed up im sorry about that Quote Link to comment Share on other sites More sharing options...
nickjonnes Posted May 30, 2011 Author Share Posted May 30, 2011 i fixed it, didnt have the $ sign at start but now im getting "unexspected T_string" <?php $dom = new DOMDocument('1.0','ISO-8859-1'); $dom->load("http://feeds.bbci.co.uk/news/video_and_audio/science_and_environment/rss.xml"); $elements = $dom->getElementsByTagName("guid"); for each ($element in $elements) {echo $element->innerHTML;} ?> Quote Link to comment Share on other sites More sharing options...
requinix Posted May 30, 2011 Share Posted May 30, 2011 Wow. Yeah. Too much C#. foreach ($elements as $element) { Quote Link to comment Share on other sites More sharing options...
salathe Posted May 30, 2011 Share Posted May 30, 2011 You might also like to use SimpleXML, which is supposed to make accessing XML "simple". To get the first link from within a feed item, you can simply do: $feed = simplexml_load_file('http://feeds.bbci.co.uk/news/video_and_audio/science_and_environment/rss.xml'); $link = (string) $feed->channel->item->link; echo $link; Be sure to read through the examples page. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.