27. november 2005 - 02:02
Der er
7 kommentarer
RSS, hvordan?
Hvordan ripper man newz.dk's rss, enten med PHP eller HTML
27. november 2005 - 03:21
#2
<?php
class RSSReader
{
private $insideitem = false;
private $tag = "";
private $title = "";
private $description = "";
private $link = "";
public $News;
public $Info;
public $Count;
function __construct($link, $encoding)
{
$xml_parser = xml_parser_create($encoding);
xml_set_object($xml_parser,&$this);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen($link,"r") or die("Error reading RSS data.");
while ($data = fread($fp, 4096))
xml_parse($xml_parser, $data, feof($fp)) or die(sprintf("XML error: %s at line %d",
xml_parser_free($xml_parser);
$this->Count = count($this->News);
}
function startElement($parser, $tagName, $attrs)
{
$this->tag = $tagName;
if ($tagName == "ITEM")
$this->insideitem = true;
}
function endElement($parser, $tagName)
{
if ($tagName == "ITEM")
{
$this->News[] = array(trim($this->link), htmlspecialchars(trim($this->title)), htmlspecialchars(trim($this->description)));
$this->title = "";
$this->description = "";
$this->link = "";
$this->insideitem = false;
}
}
function characterData($parser, $data)
{
if ($this->insideitem)
{
switch ($this->tag)
{
case "TITLE":
$this->title .= $data;
break;
case "DESCRIPTION":
$this->description .= $data;
break;
case "LINK":
$this->link .= $data;
break;
}
}
else
{
if (!isset($this->Info[strtolower($this->tag)]))
switch ($this->tag)
{
case "TITLE":
$this->Info["title"] = $data;
break;
case "DESCRIPTION":
$this->Info["description"] = $data;
break;
case "LINK":
$this->Info["link"] = $data;
break;
case "LANGUAGE":
$this->Info["language"] = $data;
break;
}
}
}
}
?>
Eksempel med koden:
<?php
require("rss.class.php");
$rss = new RSSReader("adresse", "ISO-8859-1");
print_r($rss->News);
print_r($rss->Title);
print_r($rss->Description);
print_r($rss->Count);
?>
27. november 2005 - 03:22
#3
Eksempel med koden var lige lidt forkert:
<?php
require("rss.class.php");
$rss = new RSSReader("adresse", "ISO-8859-1");
print_r($rss->News);
print_r($rss->Info);
print_r($rss->Count);
?>