PHP - Parsing Xml+atom Feeds
Hello everyone,
I am having problem trying to fetch data from certain feeds. The problem is, the feeds aren't just using XML or just atom, it is a combo of both. Here is an example of YouTube's feed: Code: [Select] <?xml version='1.0' encoding='UTF-8'?><entry xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:yt='http://gdata.youtube.com/schemas/2007'><id>http://gdata.youtube.com/feeds/api/videos/qzUuAIPPrGQ</id><published>2010-11-02T15:10:24.000Z</published><updated>2010-11-04T22:09:22.000Z</updated><category scheme='http://schemas.google.com/g/2005#kind' term='http://gdata.youtube.com/schemas/2007#video'/><category scheme='http://gdata.youtube.com/schemas/2007/categories.cat' term='Film' label='Film & Animation'/><category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='mysteryguitarman'/><category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='mystery'/><category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='guitar'/><category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='man'/><category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='mgm'/><category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='joe'/><category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='penna'/><category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='jp'/><category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='balinese'/><category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='bali'/><category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='instruments'/><title type='text'>Bali Instruments</title><content type='text'>Behind the Scenes: http://bit.ly/BehindTheScenesBali Click to Tweet: http://bit.ly/ClickToTweetBali Watch my other videos: http://www.youtube.com/MysteryGuitarMan My second channel: http://www.youtube.com/jp Twitter's cool... right? http://www.twitter.com/MysteryGuitarM Send me things: Mystery Guitar Man PO Box 2966 Hollywood, CA 90078 -Subtitles- Portuguese: http://youtube.com/ands159</content><link rel='alternate' type='text/html' href='http://www.youtube.com/watch?v=qzUuAIPPrGQ&feature=youtube_gdata'/><link rel='http://gdata.youtube.com/schemas/2007#video.responses' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/videos/qzUuAIPPrGQ/responses'/><link rel='http://gdata.youtube.com/schemas/2007#video.related' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/videos/qzUuAIPPrGQ/related'/><link rel='http://gdata.youtube.com/schemas/2007#mobile' type='text/html' href='http://m.youtube.com/details?v=qzUuAIPPrGQ'/><link rel='self' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/videos/qzUuAIPPrGQ'/><author><name>MysteryGuitarMan</name><uri>http://gdata.youtube.com/feeds/api/users/mysteryguitarman</uri></author><gd:comments><gd:feedLink href='http://gdata.youtube.com/feeds/api/videos/qzUuAIPPrGQ/comments' countHint='10222'/></gd:comments><media:group><media:category label='Film & Animation' scheme='http://gdata.youtube.com/schemas/2007/categories.cat'>Film</media:category><media:content url='http://www.youtube.com/v/qzUuAIPPrGQ?f=videos&app=youtube_gdata' type='application/x-shockwave-flash' medium='video' isDefault='true' expression='full' duration='123' yt:format='5'/><media:content url='rtsp://v6.cache6.c.youtube.com/CiILENy73wIaGQlkrM-DAC41qxMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='123' yt:format='1'/><media:content url='rtsp://v3.cache7.c.youtube.com/CiILENy73wIaGQlkrM-DAC41qxMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='123' yt:format='6'/><media:description type='plain'>Behind the Scenes: http://bit.ly/BehindTheScenesBali Click to Tweet: http://bit.ly/ClickToTweetBali Watch my other videos: http://www.youtube.com/MysteryGuitarMan My second channel: http://www.youtube.com/jp Twitter's cool... right? http://www.twitter.com/MysteryGuitarM Send me things: Mystery Guitar Man PO Box 2966 Hollywood, CA 90078 -Subtitles- Portuguese: http://youtube.com/ands159</media:description><media:keywords>mysteryguitarman, mystery, guitar, man, mgm, joe, penna, jp, balinese, bali, instruments</media:keywords><media:player url='http://www.youtube.com/watch?v=qzUuAIPPrGQ&feature=youtube_gdata_player'/><media:thumbnail url='http://i.ytimg.com/vi/qzUuAIPPrGQ/2.jpg' height='90' width='120' time='00:01:01.500'/><media:thumbnail url='http://i.ytimg.com/vi/qzUuAIPPrGQ/1.jpg' height='90' width='120' time='00:00:30.750'/><media:thumbnail url='http://i.ytimg.com/vi/qzUuAIPPrGQ/3.jpg' height='90' width='120' time='00:01:32.250'/><media:thumbnail url='http://i.ytimg.com/vi/qzUuAIPPrGQ/0.jpg' height='240' width='320' time='00:01:01.500'/><media:title type='plain'>Bali Instruments</media:title><yt:duration seconds='123'/></media:group><gd:rating average='4.970053' max='5' min='1' numRaters='40338' rel='http://schemas.google.com/g/2005#overall'/><yt:statistics favoriteCount='10811' viewCount='705760'/></entry> What do you recommend doing to getting data out of feeds such as this? Thank you, weee Similar TutorialsI'm using the following code to grab an RSS feed and parse it into an array: Code: [Select] $doc = new DOMDocument(); $doc->load('http://MySite.com/feed/'); $arrFeeds = array(); foreach ($doc->getElementsByTagName('item') as $node) { $itemRSS = array ( 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, 'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue, 'link' => $node->getElementsByTagName('link')->item(1)->nodeValue, 'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue ); array_push($arrFeeds, $itemRSS); }My problem is that there is one tag I'd like to access, but don't quite know how... It is: <content:encoded> I've looked at 'content' => $node->getElementsByTagName('content:encoded')->item(1)->nodeValue and 'content' => $node->getElementsByTagName('content')->item(1)->nodeValue But neither work. Any ideas? Hi guys, im having problem on how to insert this code ... <atom:link href="######" rel="self" type="application/rss+xml" /> Code: [Select] <?php /** A.R.A Class RSS Builder (c) 2007 Denis Sureau http://www.scriptol.com/rss/ API for creating RSS 2.0 feeds Licence GNU GPL 2. */ // Create a feed // Returns the XML document class ara { public $doc; public $rss; public $channel; function ARAMakeTag($tagname, $str) { $tag= $this->doc->createElement($tagname); $data= $this->doc->createTextNode($str); $tag->appendChild($data); return $tag; } public function ARAFeed($title, $link, $desc, $date) { $this->doc = new DOMDocument("1.0", "UTF-8"); $this->doc->formatOutput = true; $this->rss = $this->doc->createElement("rss"); $this->rss->setAttribute("version", "2.0"); $this->rss->setAttribute("xmlns:atom", "http://www.w3.org/2005/Atom"); $this->doc->appendChild($this->rss); $this->channel = $this->doc->createElement("channel"); $this->rss->appendChild($this->channel); $this->channel->appendChild($this->ARAMakeTag("title", $title)); $this->channel->appendChild($this->ARAMakeTag("link", $link)); $this->channel->appendChild($this->ARAMakeTag("description", $desc)); $this->channel->appendChild($this->ARAMakeTag("pubDate", $date)); } public function ARAItem( $title, $id, $cid, $desc, $date) { $item = $this->doc->createElement("item"); $item->appendChild($this->ARAMakeTag("title", $title)); $item->appendChild($this->ARAMakeTag("link", "####")); $item->appendChild($this->ARAMakeTag("guid", "#####")); $item->appendChild($this->ARAMakeTag("description", $desc)); $item->appendChild($this->ARAMakeTag("pubDate", $date)); $this->channel->appendChild($item); } public function save($fname) { $this->doc->save($fname); } public function saveXML() { return $this->doc->saveXML(); } } ?> i would like to add this code to validate my rss feed.. <atom:link href="http://dallas.example.com/rss.xml" rel="self" type="application/rss+xml" /> thanks in advance guys.. Hi Everyone, Please have a look at this, and tell me if its right? I am generating a sitemap for my website dynamically. The date I am providing: 2012-02-24 11:56:09 and this is the output I am getting: 2012-02-24T11:56:09Z now I am not sure of the Z at the end of the returned value. I think I am looking for something similar to this: 2012-03-12T12:00:02+02:00 not to sure what I am doing wrong and where it gets the timezone from, I am guessing from the php config file. any help would be much appreciated. thanks My code: Code: [Select] <lastmod><?php echo $time->toAtom($post['Post']['modified']); ?></lastmod> Hi i have a simple blog which is stored in a table called messages, i have tried to create an rss feed that poeple can link to but am a bit stuck. the feed does not validate with the error code of: Sorry This feed does not validate. line 15, column 2: XML parsing error: <unknown>:15:2: not well-formed (invalid token) [help] Code: [Select] $sql = "SELECT title FROM messages ORDER BY date_added DESC limit 0,10"; Anybody got any ideas on this please, have read the rss pages and can comsume a fedd ok just cant produce one. Code: [Select] <?php $host = ""; $user = ""; $pass = ""; $port = "3306"; $database = "uni"; $MySQL = mysql_connect( "$host:$port", $user, $pass ); mysql_select_db( $database, $MySQL) or die ( "Couldn't open $database: ".mysql_error() ); ?> // Get latest items from db $sql = "SELECT title FROM messages ORDER BY date_added DESC limit 0,10"; $rst = mysql_query($sql); // Loop through data and build feed items $items = ""; while($a_row = mysql_fetch_assoc($rst)) { $items.= "<item>"; $items.= " <title>{$rst['title']}</title>"; $items.= " <link>{$rst['link']}</link>"; $items.= " <description>{$rst['description']}</description>"; $items.= "</item>"; } // Build feed text $feed = "<?xml version=\"1.0\"?>"; $feed.= "<rss version=\"2.0\">"; $feed.= " <channel>"; $feed.= " <title>My cool feed</title>"; $feed.= " <link>http://mi-linux.wlv.ac.uk/~0274148/AWT/blog/index.php</link>"; $feed.= " <description>A cool feed</description>"; $feed.= " $items"; $feed.= " </channel>"; $feed.= "</rss>"; // Display feed text echo $feed; Hi again, What i am after is receiving some RSS feed from another website and displaying it on mine. Well i am not familiar with RSS feeds and how can i style them with CSS, so they match the layout of my website. I want to display the RSS feeds with PHP so they are effective with SEOS rather than using Javascript. Could some one point at a good tutorial or guide me in the right direction. It would be much appreciated thanks. I have a blog i created and i want to be able to create a page that can deal with the rss feeds. I have never done it before so if any one can give me some sugestions thats would be great. Hi, I am looking to have multiple RSS feeds coming populating into one list sorted by latest. Does anyone know or have any codes they are willing to share, even if it is just to get me started? Any help is much appreciated. This is for a charitable website so your time, help & expertise are much appreciated. If one inserts acute letters, grave letters, ampersands and accented letters in xhtml they show perfectly on the page. However if one uses insert or rss feed they show as � or a square. How does one make them show correctly. Hi, I kow this is probably something simple, but i have 2 links to a RSS feed which i need to put up on a web page. Not just a link, but the actual feed rendered with a number of news headlines. One is a ".rss2.XML" link and the ither is a html link. the HTML i suppose is static, so probably not any good. thanks in advance Currently I run a site that aggregates jobs about 100 cities so far and its starting to slow down my sites resources, I use a wordpress plugin to bring in the jobs but its giving me a resources alert.
My traffics not that high as of yet, but I'm worried about it causing problems on my shared hosting.
Server is running apache. Slow for wordpress?
Was just wondering best way to bring in multiple cities (thousands) /feeds and add category for each.
Hello, I'm using pbb forum http://xemix.pl/ When I tried to get topics using rss feeds module, I got this error. Code: [Select] Fatal error: Uncaught exception 'Exception' with message 'Erroe occured while loading url by cURL. Couldn't resolve host 'pbboard.com'' in /home/a5402323/public_html/includes/FeedParser.php:226 Stack trace: #0 /home/a5402323/public_html/includes/FeedParser.php(157): FeedParser->getUrlContent() #1 /home/a5402323/public_html/modules/admin/feeder.module.php(218): FeedParser->parse('http://absba.or...') #2 /home/a5402323/public_html/modules/admin/feeder.module.php(49): PowerBBFeederMOD->_AddFeedStart() #3 /home/a5402323/public_html/admin.php(89): PowerBBFeederMOD->run() #4 {main} thrown in /home/a5402323/public_html/includes/FeedParser.php on line 226 the host said thet Hello,cURL is installed by default here Many Thanks,Helpdesk Staff any help !!!! Hi there all, I want to pull news feeds, from a web site and display them .... I have forgot what commands to use to do this. please help. Any example, i am most grateful. Hi there, I'm wondering If anyone can assist me. I am currently a design student In Leeds, UK. I'm currently creating an online campaign whereby customers use QR codes to link to a created website with interesting youtube video on. At the moment I've just created a simple link to the chosen content http://newmedia.leeds.ac.uk/ug10/cs10dw/culture.html However I've come across Youtube API Standard feeds on the Youtube Reference Guide. http://code.google.com/apis/youtube/2.0/reference.html It states you can 'send a GET request to the URL associated with that feed', however, I've never really touched php before. Could anyone give me advise on how I would link say the Top rated feed to my site using https://gdata.youtube.com/feeds/api/standardfeeds/top_rated Would that be difficult? ANY replies would be much appreciated! Regards David. I have a full-text RSS component for Joomla that I was trying to modify. I wanted to be able to truncate the article to a given number of words and place a link at the end for the rest of the article. I need help figuring out how to add a link to the end of the source article. Added this script to the post.php file. It sort of works but the "$ending" tag doesn't seem to function since there is no "..." appended to the end of articles posted, when I tested it. public function truncate($text, $length = 200, $ending = '...', $exact = true, $considerHtml = false) { if ($considerHtml) { // if the plain text is shorter than the maximum length, return the whole text if (str_word_count(preg_replace('/<.*?>/', '', $text)) <= $length) { return $text; } // splits all html-tags to scanable lines preg_match_all('/(<.+?>)?([^<>]*)/s', $text, $lines, PREG_SET_ORDER); $total_length = str_word_count($ending); $open_tags = array(); $truncate = ''; foreach ($lines as $line_matchings) { // if there is any html-tag in this line, handle it and add it (uncounted) to the output if (!empty($line_matchings[1])) { // if it's an "empty element" with or without xhtml-conform closing slash (f.e. <br/>) if (preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1])) { // do nothing // if tag is a closing tag (f.e. </b>) } else if (preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $line_matchings[1], $tag_matchings)) { // delete tag from $open_tags list $pos = array_search($tag_matchings[1], $open_tags); if ($pos !== false) { unset($open_tags[$pos]); } // if tag is an opening tag (f.e. <b>) } else if (preg_match('/^<\s*([^\s>!]+).*?>$/s', $line_matchings[1], $tag_matchings)) { // add tag to the beginning of $open_tags list array_unshift($open_tags, strtolower($tag_matchings[1])); } // add html-tag to $truncate'd text $truncate .= $line_matchings[1]; } // calculate the length of the plain text part of the line; handle entities as one character $content_length = str_word_count(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', ' ', $line_matchings[2])); if ($total_length+$content_length> $length) { // the number of characters which are left $left = $length - $total_length; $entities_length = 0; // search for html entities if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', $line_matchings[2], $entities, PREG_OFFSET_CAPTURE)) { // calculate the real length of all entities in the legal range foreach ($entities[0] as $entity) { if ($entity[1]+1-$entities_length <= $left) { $left--; $entities_length += str_word_count($entity[0]); } else { // no more characters left break; } } } $truncate .= substr($line_matchings[2], 0, $left+$entities_length); // maximum lenght is reached, so get off the loop break; } else { $truncate .= $line_matchings[2]; $total_length += $content_length; } // if the maximum length is reached, get off the loop if($total_length>= $length) { break; } } } else { if (str_word_count($text) <= $length) { return $text; } else { $truncate = substr($text, 0, $length - str_word_count($ending)); } } // if the words shouldn't be cut in the middle... if (!$exact) { // ...search the last occurance of a space... $spacepos = strrpos($truncate, ' '); if (isset($spacepos)) { // ...and cut the text in this position $truncate = substr($truncate, 0, $spacepos); } } // add the defined ending to the text $truncate .= $ending; if($considerHtml) { // close all unclosed html-tags foreach ($open_tags as $tag) { $truncate .= '</' . $tag . '>'; } } return $truncate; } I have attached the whole file as well if you want to see the rest. This part of the script has the function that originally automatically added the link to the source article: if($link_original==1){ if(strlen($ret->fullText)) $ret->fullText.="<p><a href='". $item->link."' target='_blank'>".$text_link_original."</a></p>"; else $ret->introText.="<p><a href='". $item->link."' target='_blank'>".$text_link_original."</a></p>"; } The problem is that it gets truncated with the rest of the article now. Hi
I'm looking for some advice on how to parse an xml feed and display any odds on my site
Here is an example http://feed.youwincd...in_football.xml
and as you can see it is quite large so calling it every time would have an impact on the server.
I am looking at using many different feeds in the future so want to start this properly and in a scaleable way.
I want to use php but open to any ideas on the best way to do this
Many thanks
Sam
I'm trying to go through a document and replace all the UBB tags with different values. I can preg match the tags, but I don't quit remember how to do the replacement. Tags are like: ect. Anyone have a basic example I can go from? I know I've done it before, just can't think tonight. Basically I'm reading a local xml file, extracting data, and passing array over to a MySql function. Also, when each record is extracted I need to know the full parent-child category it was listed under. for example: <root> <node value="first"/> <node value="second"/> <header label="Numbers"> <header label="Roman numerals"> <node value="I"/> <node value="XV"/> </header> <node value="1"/> <node value="10"/> </header> </root> what is wrong with this code? I want to parse rss content and its giving error that, the input is not corret $xmlDoc = new DOMDocument(); $xml="http://www.pharmamanufacturing.com/index.html?mode=rss"; $xmlDoc->loadxml($xml); //$xmlDoc->load($xml); I need to parse XML and this article helped me a lot: http://www.phpfreaks.com/tutorial/handling-xml-data The next thing I need is to parse only elements that meet certain conditions, lets say we have an XML with structure like this: <Result> <Line> <StockCode>0101009</StockCode> <Description>Description</Description> <ProductGroup>S01</ProductGroup> <Availability>0</Availability> </Line> ... </Result> I need to parse only the elements with ProductGroup = S01 and Availability = 1 How can I do that Hello, I have an xml file below with multiple namespaces called "date". I want to know how to echo out each date that is associated with that "item" if that makes sense. Thanks Code: [Select] <?xml version="1.0" encoding="UTF-8"?> <rss version="2.0" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:dc="http://purl.org/dc/elements/1.1/"> <channel> <item> <title>My Title</title> <dc:date>2009-02-12</dc:date> <dc:date>2010-09-01</dc:date> </item> </channel> </rss> |