From eb4230d23f1199c1d3cf231d0e914fc360c40ac0 Mon Sep 17 00:00:00 2001 From: Chouchen Date: Wed, 1 Jun 2011 14:07:17 +0200 Subject: [PATCH] Added image getter --- README | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ srss.php | 31 +++++++++++++++++++++++- 2 files changed, 102 insertions(+), 1 deletion(-) diff --git a/README b/README index e69de29..8c68278 100644 --- a/README +++ b/README @@ -0,0 +1,72 @@ +All documentation @ http://labs.shikiryu.com/SRSS/#_how + +---------------------------------- + +How to make it read RSS? + +First, we need to load the RSS : + + $rss = SRSS::read('http://exemple.com/rss.xml'); + +Easy, right? Then you can extract general informations : + + echo $rss->title; // will display blog title + +Then, you can take care of articles. You can select a precise article : + + $article1 = $rss->getItem(1); // or $rss->getFirst(); + +Or looping them : + + foreach($rss as $article) + { + echo ''. SRSSTools::formatDate('d/m/y', $item->pubDate).' '.$item->title.''; + } + +If you like arrays, you can transform the RSS into an array : + + $rssArray = $rss->toArray(); + +You can also save it into your server with : + + $rss->save('/www/rss/rss.xml'); // example + +---------------------------------- + +How to make it create RSS? + +First, we need to initialize the RSS : + + $rss = SRSS::create(); + +Easy, right? Then you can add general informations : + + $rss->title = 'My Awesome Blog'; + $rss->link = 'http://shikiryu.com/devblog/'; + $rss->description = 'is awesome'; + +Those 3 are mandatory to validate your RSS, other options can be added. +Then, you can add articles. Let's imagine $content contains an array from your database. + + foreach($content as $item){ + $rssitem= new SRSSItem; // we create an item + $rssitem->title = $item["title"]; // adding title (option) + $rssitem->link = $item['link']; // adding link (option) + $rssitem->pubDate = $item["date"]; // date automatically transformed into RSS format (option) + $rssitem->description = $item["text"]; // adding description (mandatory) + $rss->addItem($rssitem); // we add the item into our RSS + } + +There are 2 functions to add item. +The first one will add items in the order you enter them, from top to bottom. + + $rss->addItem($item); + +The other one does the opposite and add the next item in top of your RSS + + $rss->addItemBefore($item); + +---------------------------------- + +Contact : +http://shikiryu.com/contact diff --git a/srss.php b/srss.php index 275bf04..5f07c29 100644 --- a/srss.php +++ b/srss.php @@ -105,6 +105,27 @@ class SRSS extends DomDocument implements Iterator return $doc; } + /** + * getter of "image"'s channel attributes + * @return string or array + */ + public function image() + { + $args = func_get_args(); + if(func_num_args() == 0) $args[0] = 'url'; + $r = array(); + if(!empty($this->attr['image'])) + { + foreach($this->attr['image'] as $key => $val) + { + if(in_array($key, $args)) + $r[$key] = $val; + } + } + else return; + return (func_num_args() > 1) ? $r : $r[$args[0]]; + } + /** * setter of "image"'s channel attributes * @param $url picture's url @@ -405,7 +426,15 @@ class SRSS extends DomDocument implements Iterator { if($child->nodeType == XML_ELEMENT_NODE && $child->nodeName != 'item') { - $this->attr[$child->nodeName] = $child->nodeValue; + if($child->nodeName == 'image'){ + foreach($child->childNodes as $children) + { + if($children->nodeType == XML_ELEMENT_NODE) + $this->attr['image'][$children->nodeName] = $children->nodeValue; + } + } + else + $this->attr[$child->nodeName] = $child->nodeValue; } } }