1

I'm trying to include an RSS feed on my website. The following code works locally but causes a fatal error on the live site:

<?php
// Initialise the cURL resource handle:
$ch = curl_init("http://www.blogs.stopjunkmail.org.uk/diary/index.php?/feeds/index.rss2");
// Set connection options:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
// Execute connection, wait for response, and close:
$data = curl_exec($ch);
curl_close($ch);
// Parse the data:
$doc = new SimpleXmlElement($data, LIBXML_NOCDATA);
// Define the function to parse RSS:
function parseRSS($doc) {
    echo '<ul>' . "\n";
    for($i=0; $i<5; $i++) {
        $url    = $doc->channel->item[$i]->link;
        $title  = $doc->channel->item[$i]->title;
        $date   = $doc->channel->item[$i]->pubDate;
        echo '<li>' . "\n";
        echo '<a href="'.$url.'">'.$title.'</a>' . "\n";
        echo '</li>' . "\n";
    }
    echo '</ul>' . "\n";
}
?>
<!doctype html>
<html lang="en-GB">
<head>
 <meta charset="UTF-8" />
 <title>Test feed</title>
</head>
<body>

 <h2>Recent blog entries</h2>
<?php parseRSS($doc); ?>

</body>
</html>

This causes the following errors:

[Thu Sep 29 12:06:28 2011] [error] [client xx.xx.xx.xxx] PHP Fatal error:  Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /home/sites/stopjunkmail.org.uk/public_html/news/_test.php:11
[Thu Sep 29 12:06:28 2011] [error] [client xx.xx.xx.xxx] Stack trace:
[Thu Sep 29 12:06:28 2011] [error] [client xx.xx.xx.xxx] #0 /home/sites/stopjunkmail.org.uk/public_html/news/_test.php(11): SimpleXMLElement->__construct('', 16384)
[Thu Sep 29 12:06:28 2011] [error] [client xx.xx.xx.xxx] #1 {main}
[Thu Sep 29 12:06:28 2011] [error] [client xx.xx.xx.xxx]   thrown in /home/sites/stopjunkmail.org.uk/public_html/news/_test.php on line 11

After lots of trial and error and looking up similar questions I've found it's the feed that's causing the problem. If I change the feed to a very basic sample feel (for instance http://feedparser.org/docs/examples/rss20.xml) it all works fine. The feed I'm trying to parse is valid (though has some warnings).

Question is... what do I need to do get the script to accept the feed?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
rkhff
  • 1,696
  • 2
  • 16
  • 16

2 Answers2

1

Change to utf8 using mb_convert_encoding() and don't forget to call the parseRSS() function.

// Initialise the cURL resource handle:
$ch = curl_init("http://www.blogs.stopjunkmail.org.uk/diary/index.php?/feeds/index.rss2");
// Set connection options:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
// Execute connection, wait for response, and close:
$data = curl_exec($ch);
curl_close($ch);
// Parse the data:
$enc = mb_detect_encoding($data);
$data = mb_convert_encoding($data, 'UTF-8', $enc);

// Define the function to parse RSS:
function parseRSS($doc) {
    echo '<ul>' . "\n";
    for($i=0; $i<5; $i++) {
        $url    = $doc->channel->item[$i]->link;
        $title  = $doc->channel->item[$i]->title;
        $date   = $doc->channel->item[$i]->pubDate;
        echo '<li>' . "\n";
        echo '<a href="'.$url.'">'.$title.'</a>' . "\n";
        echo '</li>' . "\n";
    }
    echo '</ul>' . "\n";
}
parseRSS($doc);
?>
<!doctype html>
<html lang="en-GB">
<head>
 <meta charset="UTF-8" />
 <title>Test feed</title>
</head>
<body>
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Charlie
  • 1,062
  • 6
  • 9
0

Maybe another option to force the MIME type?

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: text/xml'));

EDIT: Could also be a problem with cURL on your webserver compared to your local environment. If the PHP versions are different then that could be an issue.

David Weitz
  • 461
  • 4
  • 10
  • Didn't do the trick :(. The PHP versions are different: 5.3.2 locally vs 5.2.17 live. I've set PHP locally to 5.2.13 - the feed is still being displayed. – rkhff Sep 29 '11 at 14:47