Hola, estoy intentando leer un Rss, el código es el siguiente:
Código PHP:
class Rss{
public $file;
public $feed;
public $currently_writing;
public $main;
public $item_counter;
public function __construct()
{
$this->feed=array();
$this->this->currently_writing="";
$this->main="";
$this->item_counter=0;
}
public function startElement($parser, $name, $attrs)
{
switch($name) {
case "RSS":
case "RDF:RDF":
case "ITEMS":
$this->currently_writing = "";
break;
case "CHANNEL":
$this->main = "CHANNEL";
break;
case "IMAGE":
$this->main = "IMAGE";
$this->feed['rss']["IMAGE"] = array();
break;
case "ITEM":
$this->main = "ITEMS";
break;
default:
$this->currently_writing = $name;
break;
}
}
public function endElement($parser, $name)
{
$this->currently_writing = "";
if ($name == "ITEM") {
$this->item_counter++;
}
}
public function characterData($parser, $data)
{
if ($this->currently_writing != "") {
switch($this->main) {
case "CHANNEL":
if (isset($this->feed['rss'][$this->currently_writing])) {
$this->feed['rss'][$this->currently_writing] .= $data;
} else {
$this->feed['rss'][$this->currently_writing] = $data;
}
break;
case "IMAGE":
if (isset($this->feed['rss'][$this->main][$this->currently_writing])) {
$this->feed['rss'][$this->main][$this->currently_writing] .= $data;
} else {
$this->feed['rss'][$this->main][$this->currently_writing] = $data;
}
break;
case "ITEMS":
if (isset($this->feed['rss'][$this->main][$this->item_counter][$this->currently_writing])) {
$this->feed['rss'][$this->main][$this->item_counter][$this->currently_writing] .= $data;
} else {
$this->feed['rss'][$this->main][$this->item_counter][$this->currently_writing] = $data;
}
break;
}
}
}
function getRss($file)
{
$this->file=$file;
if(file_get_contents($this->file))
{
$xml_parser=xml_parser_create();
xml_set_element_handler($xml_parser, $this->startElement, $this->endElement);
xml_set_character_data_handler($xml_parser, $this->characterData);
if(!($fp = fopen($this->file, "r")))
die("could not open XML input");
while($data=fread($fp, 4096))
{
if (!xml_parse($xml_parser, $data, feof($fp)))
die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
}
xml_parser_free($xml_parser);
return $this->feed;
}
else
return array("error"=>"Error a leer el XML.");
}
}
$rss=new Rss();
var_dump($rss->getRss("http://www.cronicachillan.cl/cronica_chillan_base/site/edic/base/rss/portada.xml"));
Esto me retorna:
array(0) { }
Es raro, el feed esta bueno, también e probado con otros y no funciona...
saludos...