1

I am trying to load an RSS feed into my website and I found a PHP script that does this and applies some stylable tags to the content.

Below is the original script that gets the RSS feed and outputs it as stylable HTML on my site (this works locally, the RSS items get placed nicely in my site on local testserver) When I upload this to my webserver, it will not get the RSS items and it displays only empty containers where RSS content should be.

This is the rss-speech.php script:

$rss = new DOMDocument();
$rss->load('http://www.whitehouse.gov/podcast/audio/speeches/rss.xml');
$feed = array();

foreach ($rss->getElementsByTagName('item') as $node) {
    $item = array (
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
        'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
    );

    array_push($feed, $item);
}

$limit = 34;

for($x=0;$x<$limit;$x++) {
    $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
    $link = $feed[$x]['link'];
    $description = $feed[$x]['desc'];
    $date = date('l F d, Y', strtotime($feed[$x]['date']));

    echo '<div class="rssitem">';
    echo '<p><a href="'.$link.'" title="'.$title.'">'.$title.'</a><br />';
    echo '<small><em>Posted on '.$date.'</em></small></p>';
    echo '<p style=" ">'.$description.'</p>';
    echo '</div>';
}

This seems to be working fine when I test it on my local testserver (MAMP), but when I FTP the script to my remote webserver it doesn't seem to load the feed (everything else works because I can see the script trying to make the 'Posted on' lines, the content is just missing.

I thought it may have something to do with cross domain feed parsing so I tried to make a php proxy like so:

rss-proxy.php

<?php
header ("Content-Type:text/xml");

echo file_get_contents('http://www.whitehouse.gov/podcast/audio/speeches/rss.xml');
?>

If I run this, it will display in the browser the content like so:

en clean Public Domain The White House Keep up with all of President Obama's remarks, town halls, and press conferences in this comprehensive podcast. This feed will occasionally include remarks from other principals like Vice President Biden and First Lady Michelle Obama. Keep up with all of President Obama's remarks, town halls, and press conferences in this comprehensive podcast. This feed will occasional [etc...]

A sample of the XML I'm trying to parse:

<channel>
    <title>White House Speeches (Audio)</title>
    <link>http://www.whitehouse.gov/podcast/audio/press-briefings/rss.xml
        <description></description>
        <language>en</language>
        <atom:link href="http://www.whitehouse.gov/podcast/audio/speeches/rss.xml" rel="self" 
           type="application/rss+xml">
           <itunes:explicit>clean</itunes:explicit>
           <copyright>Public Domain</copyright>
           <itunes:author>The White House</itunes:author>
           <itunes:image href="http://www.whitehouse.gov/sites/default/files/imagecache/podcast_detail
               /podcasts/audio/speeches_audio.jpg">
               <itunes:subtitle>Keep up with all of President Obama's remarks, town halls, and press 
                   conferences in this comprehensive podcast.  This feed will occasionally include remarks from 
                   other principals like Vice President Biden and First Lady Michelle Obama.</itunes:subtitle>
                   <itunes:summary>Keep up with all of President Obama's remarks, town halls, and press 
                       conferences in this comprehensive podcast.  This feed will occasionally include remarks from    
                       other principals like Vice President Biden and First Lady Michelle Obama.</itunes:summary>
                       <itunes:category text="Government &amp; Organizations">
                        <item>
                           <title>Young African Leaders Initiative Town Hall</title>
                           <link>http://www.whitehouse.gov/photos-and-video/video/2013/06/29/young-african-leaders-
                               initiative-town-hall
    <description><!--[CDATA[At the University of Johannesburg - Soweto, President Obama discusses 
       youth empowerment and leadership with young African leaders in a town hall meeting.]]-->
    </description>
    <enclosure url="http://www.whitehouse.gov/videos/2013/June/062913_UniversityofJohannesburg.mp3" type="audio/mpeg">
       <category domain="http://www.whitehouse.gov/taxonomy/term/16">The President</category>
       <category domain="http://www.whitehouse.gov/admin/category/issue-tag/foreign-policy">
           Foreign Policy</category>
           <category domain="http://www.whitehouse.gov/taxonomy/term/9">Speeches &amp; Events</category>
           <itunes:keywords>Foreign Policy</itunes:keywords>
           <itunes:author>The White House</itunes:author>
           <itunes:summary>
               <itunes:subtitle>At the University of Johannesburg - Soweto, President Obama discusses 
                   youth empowerment and leadership with young African leaders in a town hall meeting.         
               </itunes:subtitle>
               <itunes:explicit>clean</itunes:explicit>
               <pubdate>Sat, 29 Jun 2013 21:26:37 +0000</pubdate>
               <dc:creator>The White House</dc:creator>
               <guid ispermalink="false">223196 at http://www.whitehouse.gov</guid>
           </itunes:summary>
       </enclosure>
   </item>
</channel>

And then letting the rss-speech.php script do:

$rss->load('rss-proxy.php');

This is where it goes wrong, the new DOMdocument doesnt seem to get anything from the rss-proxy.php script, even though I just confirmed the proxy is correctly getting the XML source code.

I also tried this: http://benalman.com/projects/php-simple-proxy/

I even tried caching the RSS feed to an XML file in a cache directory by using this: http://www.javascriptkit.com/dhtmltutors/ajaxticker/ajaxticker2.shtml

This seems to create the XML file correctly locally, but remotely it makes a 0kb empty XML file. I already checked the permissions via FTP to be read/write for all. Also, if I try to send the content from this file to the rss-speech.php script, it doesn't work (locally or remotely).

There seems to give a lot of parsing errors as well, and it's only making 0kb empty cached RSS documents.

Nothing I try seems to work, it just won't grab the content from the RSS feed.

Any ideas on how I can get this to work?

Bojangles
  • 99,427
  • 50
  • 170
  • 208
phwoomp
  • 69
  • 1
  • 8
  • Can you post a sample of the parsing errors, as well as a cut down example of the XML you're trying to parse? – Bojangles Jun 28 '13 at 13:07
  • what's the output of this: `` ? – iguider Jun 28 '13 at 13:10
  • I hope I understood correctly what you're asking me @Bojangles. Also I am sorry but I can't really post a lot of code since I seem to be limited to 550 characters when typing a comment. I have a php script that caches the feed locally, when I run it with the correct url parameter, it outputs a xml file in a folder named 'cache'. I'd like the php script rss-speech.php (from my first post) to use this xml file for the content. Like it would normally do from the url http://www.whitehouse.gov/podcast/audio/speeches/rss.xml But when I make the line from the rss-speech.php like this: – phwoomp Jun 30 '13 at 18:48
  • `$rss->load('http://localhost/cache/http%3A%2F%2Fwww.whitehouse.gov%2Fpodcast%2Faudio%2Fspeeches%2Frss.xml');` It won't do anything. The xml file has content but the php script doesnt take it from the file. It does work however if I set it to the remote feed directly: `$rss->load('http://www.whitehouse.gov/podcast/audio/speeches/rss.xml');` But only on my local server. So when I upload my whole site through ftp and use the same script, it again shows up with an empty rss feed. – phwoomp Jun 30 '13 at 18:50
  • 1
    There's an [edit](http://stackoverflow.com/posts/17365328/edit) link under your question - you can add all the code you need to your question like that `:)` – Bojangles Jun 30 '13 at 18:53
  • @IderAghbal When I do this: `` It says it doesnt work. But I dont really understand what this script does, it the md5 number supposed to be the same as the example you posted? – phwoomp Jun 30 '13 at 18:55
  • @Bojangles Ok so I added some extra EDITs to my initial post. Hope this is what you meant when you asked which XML I want to use. – phwoomp Jun 30 '13 at 19:18
  • That's good detail, thanks. Your XML is invalid (missing closing tags) which might be throwing the parser. You mentioned lots of parse errors - read through them and manually correct a bit of test XML to see if your server can parse that. Is the PHP version of your server older than your local install? There might be a bug somewhere – Bojangles Jun 30 '13 at 19:27
  • I am sorry for being unclear, and thanks for cleaning up my messy post (I think you can see I'm new at stackoverflow `;)`) I made a mistake when I posted the XML I did a 'show selection source'. When I look at the source of the whole page it starts like this: ` White House Speeches (Audio)` – phwoomp Jun 30 '13 at 19:31
  • When I run the rss-proxy script locally, it will show me the rss feed in the normal way when you open a rss feed in a browser. But if I upload this script with ftp and I run it remotely: http://thepresidentsspeech.net/rss-proxy.php?id=WH It will tell me this: XML Parsing Error: no element found Location: http://www.thepresidentsspeech.net/rss-proxy.php?id=WH Line Number 1, Column 1: ^ Also the php version on the webserver is 5.3.26 – phwoomp Jun 30 '13 at 19:36
  • Also, if I do this: `` It will show me the right content locally, remotely it creates a blank page with no source code. – phwoomp Jun 30 '13 at 19:39

1 Answers1

1

I solved my problem!

I used cURL instead of file_get_contents.

I found out the problem was due to allow_url_fopen being disabled on my hosting server. Then I found a great tutorial about how to use cURL to read feeds.

http://ditio.net/2008/06/19/using-php-curl-to-read-rss-feed-xml/

By the way thanks for all your help!

phwoomp
  • 69
  • 1
  • 8
  • I think before you do anything else with PHP, you should read this very important Q&A on SO: [How to get useful error messages in PHP?](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php), then the first thing you should do is enable error logging on your server and learn where you find it and how you can trace it. Good luck! – hakre Jun 30 '13 at 21:30
  • Will do! Thanks a lot for being interested! – phwoomp Jun 30 '13 at 23:26