1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
5 * $Id: feed.php 3917 2009-01-21 03:06:22Z zombor $
9 * @copyright (c) 2007-2008 Kohana Team
10 * @license http://kohanaphp.com/license.html
15 * Parses a remote feed into an array.
17 * @param string remote feed URL
18 * @param integer item limit to fetch
21 public static function parse($feed, $limit = 0)
23 // Make limit an integer
24 $limit = (int) $limit;
26 // Disable error reporting while opening the feed
27 $ER = error_reporting(0);
29 // Allow loading by filename or raw XML string
30 $load = (is_file($feed) OR valid
::url($feed)) ?
'simplexml_load_file' : 'simplexml_load_string';
33 $feed = $load($feed, 'SimpleXMLElement', LIBXML_NOCDATA
);
35 // Restore error reporting
38 // Feed could not be loaded
42 // Detect the feed type. RSS 1.0/2.0 and Atom 1.0 are supported.
43 $feed = isset($feed->channel
) ?
$feed->xpath('//item') : $feed->entry
;
48 foreach ($feed as $item)
50 if ($limit > 0 AND $i++
=== $limit)
53 $items[] = (array) $item;
60 * Creates a feed from the given parameters.
62 * @param array feed information
63 * @param array items to add to the feed
66 public static function create($info, $items, $format = 'rss2')
68 $info +
= array('title' => 'Generated Feed', 'link' => '', 'generator' => 'KohanaPHP');
70 $feed = '<?xml version="1.0"?><rss version="2.0"><channel></channel></rss>';
71 $feed = simplexml_load_string($feed);
73 foreach ($info as $name => $value)
75 if (($name === 'pubDate' OR $name === 'lastBuildDate') AND (is_int($value) OR ctype_digit($value)))
77 // Convert timestamps to RFC 822 formatted dates
78 $value = date(DATE_RFC822
, $value);
80 elseif (($name === 'link' OR $name === 'docs') AND strpos($value, '://') === FALSE)
82 // Convert URIs to URLs
83 $value = url
::site($value, 'http');
86 // Add the info to the channel
87 $feed->channel
->addChild($name, $value);
90 foreach ($items as $item)
92 // Add the item to the channel
93 $row = $feed->channel
->addChild('item');
95 foreach ($item as $name => $value)
97 if ($name === 'pubDate' AND (is_int($value) OR ctype_digit($value)))
99 // Convert timestamps to RFC 822 formatted dates
100 $value = date(DATE_RFC822
, $value);
102 elseif (($name === 'link' OR $name === 'guid') AND strpos($value, '://') === FALSE)
104 // Convert URIs to URLs
105 $value = url
::site($value, 'http');
108 // Add the info to the row
109 $row->addChild($name, $value);
113 return $feed->asXML();