<?php
class rss_parser
{
public $rss;
public $alamat;
public $n = 0;
public function set_alamat($alamat)
{
$this->alamat = $alamat;
}
public function set_n($n)
{
$this->n = $n;
}
public function url_exists()
{
$AgetHeaders = @get_headers($this->alamat);
return (preg_match("|200|", $AgetHeaders[0])) ? true : false;
}
public function get_rss()
{
$p = xml_parser_create();
if ($this->url_exists())
{
xml_parse_into_struct($p, file_get_contents($this->alamat), $results, $index);
xml_parser_free($p);
$item = false;
$inc = false;
$this->rss = array();
$id_rss = 0;
foreach ($results as $idx => $val)
{
if ($val[tag] == "ITEM" && $item == false) $item = true;
if($val[tag] == "RSS")
{
$id_rss++;
$inc = true;
list($idx, $val) = each($results);
}
elseif ($val[tag] == "CHANNEL" && $val[type] == "cdata")
{
$id_rss++;
$inc = true;
list($idx, $val) = each($results);
}
elseif (!($val[tag] == "CHANNEL" || $val[tag] == "ITEM"))
{
if (sizeof($this->rss) > $this->n && $inc) break;
if (!$item && $inc) $id_rss--;
$this->rss[$id_rss][$val[tag]] = $val[value];
$inc = false;
}
}
}
}
public function cetak()
{
if (is_array($this->rss))
{
echo "<table border=\"0\">";
$n = ($this->n == 0) ? sizeof($this->rss) : $this->n;
reset($this->rss);
for ($i = 0; $i < = $n; $i++)
{
list($idx, $val) = each($this->rss);
if ($idx != 0) echo "<tr><td><font size=\"2\"><a href=\"" . trim($val[LINK]) . "\"><b>" . trim($val[TITLE]) . "</b></a>, " . substr($val[PUBDATE], 0, 16) . "<br/>" . trim($val[DESCRIPTION]) . "</font></td></tr>";
}
echo "</table>";
}
}
}
?>
Tidak ada komentar:
Posting Komentar