Tema: Leyendo rss
Ver Mensaje Individual
  #1 (permalink)  
Antiguo 12/07/2011, 07:28
geminis19
 
Fecha de Ingreso: febrero-2006
Mensajes: 58
Antigüedad: 18 años, 2 meses
Puntos: 0
Leyendo rss

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($fp4096))
            {
                if (!
xml_parse($xml_parser$datafeof($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...