54d9cad99cd22ed39f9fc810c92002a34f3d7b32
1 <?php
3 class rssReader{
5 public static $attributes = array(
6 'title','link','description','language','copyright','skipHours','timestamp',
7 'managingEditor','webMaster','pubDate','lastBuildDate','category',
8 'generator','docs','cloud','ttl','image','rating','textInput','skipDays');
10 public static function feadToArray($urls)
11 {
12 $entries = array();
14 // We support multiple urls at once.
15 if(!is_array($urls)) $urls = array($urls);
17 foreach($urls as $url){
18 $doc = new DOMDocument();
19 $doc->load($url);
20 foreach ($doc->getElementsByTagName('item') as $item) {
22 // Collect data from feed
23 $entry = array();
24 $entry['url'] = $url;
25 foreach(self::$attributes as $attr){
26 $entry[$attr] =NULL;
27 $obj = $item->getElementsByTagName($attr);
28 if(is_object($obj->item(0))){
29 $entry[$attr] = $obj->item(0)->nodeValue;
30 }
31 }
33 // Fake timestamp in none is given.
34 if($entry['pubDate'] == NULL){
35 $entry['timestamp'] = NULL;
36 }else{
38 // Create an entry timestamp
39 $entry['timestamp'] = strtotime($entry['pubDate']);
40 }
41 $entries[] = $entry;
42 }
43 }
44 return($entries);
45 }
47 public static function sortFeedResultBy($feedRes , $sortBy = 'timestamp')
48 {
50 // Do not try to sort for invalid attributes.
51 if(!in_array($sortBy, self::$attributes)){
52 trigger_error("Invalid sortby attribute '{$sortBy}'!");
53 return($feedRes);
54 }
56 // Prepare feeds to be sorted, put them in an array indexed by the 'sortBy' attribute.
57 $data = array();
58 foreach($feedRes as $feed){
59 $key = "{$feed[$sortBy]}";
60 $c = '1';
61 while(empty($key) || isset($data[$key])){
62 $key = "{$c}_{$feed[$sortBy]}";
63 $c++;
64 }
65 $data[$key] = $feed;
66 }
68 // Sort the natural way and return the results.
69 uksort($data, 'strnatcasecmp');
70 return($data);
71 }
73 }
75 ?>