Welcome Guest, Not a member yet? Register   Sign In
Trouble reading XML/RSS files, any help appreciated!
#1

[eluser]jonhurlock[/eluser]
Ive created a function which gets the latest news from different sites XML/RSS feeds, in this case i've done an example using a Twitter RSS feed, for a twitter search query, it then displays the results from the RSS feed on my site.

Basically i randomly keep on getting errors which looks like the following:
Quote:A PHP Error was encountered

Severity: Warning

Message: DOMDocument::load() [function.DOMDocument-load]: Opening and ending tag mismatch: META line 8 and HEAD in http://search.twitter.com/search.atom?q=codeigniter, line: 10

Filename: controllers/news.php

Line Number: 103
When the errors render the line number which is in red (10) keeps on changing. On one load of the page i had several of the same messages but where it displays for lines 10,11,12,13,13,13,13 (I dont understand why it rendered 13 4 times?)

The line 103 refers to the line
Code:
$xmlDoc->load($xml);
after the else statement.

Is this because the XML did not comply to specifications/is broken or is there something wrong with my code? Because some times the page appears to be fine with no errors, and then every once in a while i get spammed with errors Sad Any help would be much appreciated.

Eventually i would like to cache the results so my users aren't sending off to many requests to feeds original source

Function from my Controller:
Code:
function output_from_feed(){
    
        // feed to get
        $xml=("http://search.twitter.com/search.atom?q=codeigniter");
        $data['attribution'] = 'twitter';

        // try to read twitter's rss feed
        if(!$handle = fopen($xml, 'r'))
        {
            // couldn't open the file
            
            // could not open file, so revert to an old file on local file system
            $xml='http://www.mysite.info/assets/twitter.xml';
            // tell us where xml comes from
            $data['source'] = '<!-- source: own site -->';
            
        }
        else
        {
            // could open the file :D
            
            // get the contents of twitters xml feed
            $contents = stream_get_contents($handle);
            // close the file handler
            fclose($handle);
            
            // save twitters xml file to local file system
            
            // select file to save to
            $myFile = "./assets/twitter.xml";
            // open file, with write permission
            $fh = fopen($myFile, 'w') or die("can't open file");
            // write contents to the file
            fwrite($fh, $contents);
            // close file handler
            fclose($fh);
            
            // tell us where xml comes from    
            $data['source'] = '<!-- source: from twitter -->';
        }

        // now parse the file
        $xmlDoc = new DOMDocument();
        $xmlDoc->load($xml);
        // select tags which are entries
        $params=$xmlDoc->getElementsByTagName('entry');
        
        // format the feed.
        $data['thefeed'] = '';
        $k = 0;
        // loop those entries
        foreach ($params as $param)
        {
            // grab the tweets title and the date it was published
            $item_title=$params->item($k)->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
            $item_date=$params->item($k)->getElementsByTagName('published')->item(0)->childNodes->item(0)->nodeValue;
            
            $data['thefeed'] .="\n <li>".$item_title." published:".$item_date."</li>";
            $k++;
        }    
        // now print those tweets :D
        $this->load->view('get_news', $data);
    }
#2

[eluser]tomcode[/eluser]
To avoid getting errors :

Code:
try
{
    $xmlDoc->load($xml);
}
catch (Exception $e)
{
    // put here Your error handling code    
   echo $e->getMessage();
}

Check on parsing errors.

Check the presence of the nodes You use.
#3

[eluser]jonhurlock[/eluser]
Try Catch Blocks makes, sense cheers for the suggestion. Wink I'm sure that should sort it out. Thanks for the hand.




Theme © iAndrew 2016 - Forum software by © MyBB