/ Published in: PHP
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<? class Feed2 extends feed { /** * Creates a feed from the given parameters. * * @param array feed information * @param array items to add to the feed * @param string define which format to use * @param string define which encoding to use * @return string */ public static function create($info, $items, $format = 'rss2', $encoding = 'UTF-8') { // handle namespaces $namespaceString = ''; foreach($info['namespaces'] as $prefix => $ns) { // can't add namespaces using the api bc it adds in xmlns:xmlns="..." which is explicitly declared as invalid xml: http://www.w3.org/TR/REC-xml-names/#xmlReserved // $feed->addAttribute('xmlns:'.$prefix, $ns, 'http://www.w3.org/2000/xmlns/'); $namespaceString .= " xmlns:".$prefix.'="'.$ns.'"'; } $namespaces = $info['namespaces']; // this is so it is not included in the rest of the $info generation } $feed = '<?xml version="1.0" encoding="'.$encoding.'"?><rss version="2.0"'.$namespaceString.'><channel></channel></rss>'; foreach ($info as $name => $value) { { // Convert timestamps to RFC 822 formatted dates } { // Convert URIs to URLs $value = url::site($value, 'http'); } // Add the info to the channel $feed->channel->addChild($name, $value); } foreach ($items as $item) { // Add the item to the channel $row = $feed->channel->addChild('item'); foreach ($item as $name => $value) { { // Convert timestamps to RFC 822 formatted dates } { // Convert URIs to URLs $value = url::site($value, 'http'); } // Add the info to the row $row->addChild($name, $value); } else { $child = $row->addChild($name); // If the value is an array we are specifying attributes foreach($value as $attribute => $attributeValue) { } else { $ns = ""; } //!empty($ns) ? $namespaces[$ns] : NULL } } } } return $feed->asXML(); } } // Example Feed Generation (For Sparkle Update System): foreach(ORM::factory('updates')->find_all() as $clientUpdate) { 'title' => 'v'.$clientUpdate->version, 'url' => 'http://domain.com/', 'type' => 'application/octet-stream', 'sparkle:dsaSignature' => 'dsasig', 'sparkle:version' => '200', 'sparkle:shortVersionString' => '1.0', 'sparkle:minimumSystemVersion' => '10.6' ) ); } $feedData = feed2::create( 'title' => 'Update Feed', 'author' => 'Michael Bianco', 'link' => 'http://domain.com/', 'language' => 'en', 'sparkle' => 'http://www.andymatuschak.org/xml-namespaces/sparkle', 'dc' => 'http://purl.org/dc/elements/1.1/' ) ), $feedItems ); ?>