<?php
/* Softafzar RSS reader
* http://softafzar.net
*/
class RSS
{
private $xmlDoc = null;
private $channel = null;
private $x = null;
public function __construct ($url)
{
$this->xmlDoc = new DOMDocument();
$this->xmlDoc->load($url);
$this->x = $this->xmlDoc->getElementsByTagName('item');
}
public function itemcount ()
{
$count = 0;
foreach ($this->xmlDoc->getElementsByTagName('item') as $value) {
$count ++;
}
return $count;
}
public function get_elem_by_name($elem,$item)
{
$val = $this->x->item($item)->getElementsByTagName($elem)
->item(0)->childNodes
->item(0)->nodeValue;
return $val;
}
public function get_attr_by_elem_name($elem,$attr,$item)
{
$val = $this->x->item($item)->getElementsByTagName($elem)
->item(0)->getAttribute($attr);
return $val;
}
public function title ($item)
{
$title = $this->x->item($item)->getElementsByTagName('title')
->item(0)->childNodes
->item(0)->nodeValue;
return $title;
}
public function link ($item)
{
$link = $this->x->item($item)->
getElementsByTagName('link')->item(0)->
childNodes->item(0)->nodeValue;
return $link;
}
public function description ($item)
{
$desc = $this->x->item($item)
->getElementsByTagName('description')
->item(0)->childNodes->item(0)->nodeValue;
return $desc;
}
}
?>
مثال نحوه استفاده:
$RSS = new RSS ( "http://www.softafzar.net/external.php?type=RSS2" );
$item=0;
$url = $RSS->link ( $item );
$title = $RSS->title ( $item );
$desc = $RSS->description ( $item );
$creator= $RSS->get_elem_by_name ('creator', $item );
$date = $RSS->get_elem_by_name ('pubDate', $item );
$cat = $RSS->get_elem_by_name ('category', $item );
$caturl = $RSS->get_attr_by_elem_name('category','domain',$item);