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(' & ', ' & ', $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 & 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 & 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?