root/trunk/common/rssparser.php

Revision 533, 2.8 KB (checked in by michiel, 16 months ago)

added basic rssparser based on rss2mysql by fboender
original work can be found here: http://www.electricmonk.nl/index.php?page=SmallProgrammings#rss2mysql

Line 
1<?php
2class RSSParser {
3
4    var $saveItems = array(
5        "title"       => "string",
6        "link"        => "string",
7        "description" => "string",
8        "dc:creator"  => "string",
9        "dc:date"     => "date",
10        "dc:subject"  => "string",
11    );
12
13    /**
14     * Read and parse a RSS feed which $url points at (may be an URL or local file)
15     * @param $url string containing the URL to the RSS feed. May be an URL or local file
16     */
17    function RSSParser($rssdata) {
18        $this->rssData = $rssdata;
19        $this->readItems();
20
21    }
22
23    /**
24     * Construct a new empty RSS item
25     * @return a new empty assoc. array representing an rss item
26     */
27    function newItem() {
28        $retItem = array();
29
30        foreach(array_keys($this->saveItems) as $key) {
31            $retItem[$key] = "";
32        }
33
34        return($retItem);
35    }
36
37    /**
38     * Read <item> segments from the RSS file and stuff them in an array.
39     */
40    function readItems() {
41        if (!isset($this->rssData)) {
42            return(-1);
43        }
44
45        $this->rssItems = array();
46
47        $parser = xml_parser_create();
48
49        xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
50        xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);
51
52        xml_parse_into_struct($parser,$this->rssData,$values,$tags);
53        xml_parser_free($parser);
54
55        // Loop through all the elements in the RSS XML file.  If an <item> tag
56        // is found, it's children will be added to a array until the closing
57        // tag is found. Then the array is added to a list of items
58        // ($this->rssItems).
59        for ($i=1; $i < count($values); $i++) {
60
61            $tagName = "";
62            $tagType = "";
63            $tagValue = "";
64
65            if (array_key_exists("tag", $values[$i])) {
66                $tagName = $values[$i]["tag"];
67            }
68            if (array_key_exists("type", $values[$i])) {
69                $tagType = $values[$i]["type"];
70            }
71            if (array_key_exists("value", $values[$i])) {
72                $tagValue = $values[$i]["value"];
73            }
74
75            if ($values[$i]["tag"] == "item" && $values[$i]["type"] == "open") {
76                // Looks like we found an <item> tag. Create a new array to
77                // store it's children values as they will be found on the next
78                // iteration of the loop.
79                $rssItem = $this->newItem();
80            }
81            if ($values[$i]["tag"] == "item" && $values[$i]["type"] == "close" && isset($rssItem)) {
82                // </item> tag closed. Store the read item information.
83                $this->rssItems[] = $rssItem;
84                unset($rssItem); // No item information will be saved when this doesn't exist.
85            }
86
87            if (array_key_exists($tagName, $this->saveItems) && isset($rssItem)) {
88                // Found a tag that we want to store and that's part of an
89                // <item>. Save it.
90                switch($this->saveItems[$tagName]) {
91                    case "string":
92                        $rssItem[$tagName] = $tagValue;
93                        break;
94                    case "date":
95                        $rssItem[$tagName] = strtotime($tagValue);
96                        break;
97                    default:
98                        print("Don't know how to handle type ".$this->saveItems[$tagName].". Aborting.");
99                        exit(1);
100                        break;
101                }
102            }
103        }
104
105    }
106}
107?>
Note: See TracBrowser for help on using the browser.