xml - Rss parse error with PHP -
when try fetch , parse indian express rss feed, following error occurred.
a php error encountered
severity: warning
message: illegal string offset 'content'
filename: libraries/rssparser.php(114) : eval()'d code
line number: 1a php error encountered
severity: warning
message: illegal string offset 'encoded'
filename: libraries/rssparser.php(114) : eval()'d code
line number: 1a php error encountered
severity: warning
message: illegal string offset 'content'
filename: libraries/rssparser.php(114) : eval()'d code
line number: 1
i using rssparser library
rssparser.php
<?php // original php code chirp internet: www.chirp.com.au // please acknowledge use of code including header. class rssparser { // keeps track of current , preceding elements var $tags = array(); // array containing feed data var $output = array(); // return value display functions var $retval = ""; var $errorlevel = 0; // constructor new object function rssparser1($file) { $errorlevel = error_reporting(); error_reporting($errorlevel & ~e_notice); // instantiate xml-parser , assign event handlers $xml_parser = xml_parser_create(""); xml_set_object($xml_parser, $this); xml_set_element_handler($xml_parser, "startelement", "endelement"); xml_set_character_data_handler($xml_parser, "parsedata"); // open file reading , send data xml-parser $data = preg_match("/^http/", $file) ? file_get_contents($file) : file_get_contents($file); //print_r($data); xml_parse($xml_parser, $data) or die( sprintf("airtel: error <b>%s</b> @ line <b>%d</b><br>", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)) ); // dismiss xml parser xml_parser_free($xml_parser); error_reporting($errorlevel); } function startelement($parser, $tagname, $attrs=array()) { // rss 2.0 - enclosure if($tagname == "enclosure" && $attrs) { $this->startelement($parser, "enclosure"); foreach($attrs $attr => $attrval) { $this->startelement($parser, $attr); $this->parsedata($parser, $attrval); $this->endelement($parser, $attr); } $this->endelement($parser, "enclosure"); } // yahoo! media rss - images if($tagname == "media:content" && $attrs['url'] && $attrs['medium'] == 'image') { $this->startelement($parser, "image"); $this->parsedata($parser, $attrs['url']); $this->endelement($parser, "image"); } // check if element can contain others - list may edited if(preg_match("/^(rdf|rss|channel|image|item)/", $tagname)) { if($this->tags) { $depth = count($this->tags); if(is_array($tmp = end($this->tags))) { list($parent, $num) = each($tmp); if($parent) $this->tags[$depth-1][$parent][$tagname]++; } } array_push($this->tags, array($tagname => array())); } else { if(!preg_match("/^(a|b|i)$/", $tagname)) { // add tag tags array array_push($this->tags, $tagname); } } } function endelement($parser, $tagname) { if(!preg_match("/^(a|b|i)$/", $tagname)) { // remove tag tags array array_pop($this->tags); } } function parsedata($parser, $data) { // return if data contains no text if(!trim($data)) return; $evalcode = "\$this->output"; foreach($this->tags $tag) { if(is_array($tag)) { list($tagname, $indexes) = each($tag); $evalcode .= "[\"$tagname\"]"; if(${$tagname}) $evalcode .= "[" . (${$tagname} - 1) . "]"; if($indexes) extract($indexes); } else { if(preg_match("/^([a-z]+):([a-z]+)$/", $tag, $matches)) { $evalcode .= "[\"$matches[1]\"][\"$matches[2]\"]"; } else { $evalcode .= "[\"$tag\"]"; } } } eval("$evalcode = $evalcode . '" . addslashes($data) . "';"); } // display single channel html function display_channel($data, $limit) { extract($data); if(@$image) { // display channel image(s) foreach($image $image) $this->display_image($image); } if(@$title) { // display channel information $this->retval .= "<h1>"; if($link) $this->retval .= "<a href=\"$link\" target=\"_blank\">"; $this->retval .= stripslashes($title); if($link) $this->retval .= "</a>"; $this->retval .= "</h1>\n"; if($description) $this->retval .= "<p>$description</p>\n\n"; $tmp = array(); if(@$pubdate) $tmp[] = "<small>published: $pubdate</small>"; if(@$copyright) $tmp[] = "<small>copyright: @$copyright</small>"; if($tmp) $this->retval .= "<p>" . implode("<br>\n", $tmp) . "</p>\n\n"; $this->retval .= "<div class=\"divider\"><!-- --></div>\n\n"; } if($item) { // display channel item(s) foreach($item $item) { $this->display_item($item, "channel"); if(is_int($limit) && --$limit <= 0) break; } } } // display single image html function display_image($data, $parent="") { extract($data); if(!$url) return; $this->retval .= "<p>"; if($link) $this->retval .= "<a href=\"$link\" target=\"_blank\">"; $this->retval .= "<img src=\"$url\""; if(@$width && $height) $this->retval .= " width=\"$width\" height=\"$height\""; $this->retval .= " border=\"0\" alt=\"$title\">"; if($link) $this->retval .= "</a>"; $this->retval .= "</p>\n\n"; } // display single item html function display_item($data, $parent) { extract($data); //print_r($data); //exit; if(!$title) return; $this->retval .= "<p><b>"; if($link) $this->retval .= "<a href=\"$link\" target=\"_blank\">"; $this->retval .= stripslashes($title); if($link) $this->retval .= "</a>"; $this->retval .= "</b>"; if(!$pubdate && $dc["date"]) $pubdate = $dc["date"]; if($pubdate) $this->retval .= " <small>($pubdate)</small>"; $this->retval .= "</p>\n"; // use feed-formatted html if provided if(@$content['encoded']) { $this->retval .= "<p>" . stripslashes(@$content['encoded']) . "</p>\n"; } elseif($description) { if(@$image) { foreach($image $img) $this->retval .= "<img src=\"$img\">\n"; } $this->retval .= "<p>" . stripslashes($description) . "</p>\n\n"; } // rss 2.0 - enclosure if(@$enclosure) { $this->retval .= "<p><small><b>media:</b> <a href=\"{$enclosure['url']}\">"; @$this->retval .= $enclosure['type']; @$this->retval .= "</a> ({$enclosure['length']} bytes)</small></p>\n\n"; } if(@$comments) { $this->retval .= "<p style=\"text-align: right;\"><small>"; $this->retval .= "<a href=\"$comments\">comments</a>"; $this->retval .= "</small></p>\n\n"; } } function fixencoding(&$input, $key, $output_encoding) { if(!function_exists('mb_detect_encoding')) return $input; $encoding = mb_detect_encoding($input); switch($encoding) { case 'ascii': case $output_encoding: break; case '': $input = mb_convert_encoding($input, $output_encoding); break; default: $input = mb_convert_encoding($input, $output_encoding, $encoding); } } // display entire feed html function getoutput($limit=false, $output_encoding='utf-8') { $this->retval = ""; $start_tag = key($this->output); switch($start_tag) { case "rss": // new format - channel contains foreach($this->output[$start_tag]["channel"] $channel) { $this->display_channel($channel, $limit); } break; case "rdf:rdf": // old format - channel , items separate if(isset($this->output[$start_tag]['image'])) { foreach($this->output[$start_tag]['image'] $image) { $this->display_image($image); } } foreach($this->output[$start_tag]['channel'] $channel) { $this->display_channel($channel, $limit); } foreach($this->output[$start_tag]['item'] $item) { $this->display_item($item, $start_tag); } break; case "html": die("error: cannot parse html document rss"); default: die("error: unrecognized start tag '$start_tag' in getoutput()"); } if($this->retval && is_array($this->retval)) { array_walk_recursive($this->retval, 'myrssparser::fixencoding', $output_encoding); } //return $this->retval; //echo $this->output[$start_tag]['image']; return $this->output[$start_tag]["channel"]; } // return raw data array function getrawoutput($output_encoding='utf-8') { array_walk_recursive($this->output, 'myrssparser::fixencoding', $output_encoding); return $this->output; } } ?>
Comments
Post a Comment