Jump to content

Recommended Posts

Posted

Hi,

I would like to make an RSS feed reader on my site using PHP. I tried using the W3School's tutorial (http://www.w3schools.com/Php/php_ajax_rss_reader.asp), but I don't want users to select an rss feed. I tried to modify the scripts myself, but PHP returned multiple errors. I have tried a few RSS widgets such as Google and Feedwind, but they either didn't display the feed or didn't display the text correctly. I searched Softaculous as well, but the RSS scripts they have doesn't provide the functionality I am looking for.

Guest Geoff
Posted

PHP File:

 

<?php
//get the q parameter from URL
$q=$_GET["q"];

//find out which feed was selected
if($q=="RSS")
  {
  $xml=("http://path.to/your/feed.xml");
  }

$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);

//get elements from "<channel>"
$channel=$xmlDoc->getElementsByTagName('channel')->item(0);
$channel_title = $channel->getElementsByTagName('title')
->item(0)->childNodes->item(0)->nodeValue;
$channel_link = $channel->getElementsByTagName('link')
->item(0)->childNodes->item(0)->nodeValue;
$channel_desc = $channel->getElementsByTagName('description')
->item(0)->childNodes->item(0)->nodeValue;

//output elements from "<channel>"
echo("<p><a href='" . $channel_link
  . "'>" . $channel_title . "</a>");
echo("<br />");
echo($channel_desc . "</p>");

//get and output "<item>" elements
$x=$xmlDoc->getElementsByTagName('item');
for ($i=0; $i<=2; $i++)
  {
  $item_title=$x->item($i)->getElementsByTagName('title')
  ->item(0)->childNodes->item(0)->nodeValue;
  $item_link=$x->item($i)->getElementsByTagName('link')
  ->item(0)->childNodes->item(0)->nodeValue;
  $item_desc=$x->item($i)->getElementsByTagName('description')
  ->item(0)->childNodes->item(0)->nodeValue;

  echo ("<p><a href='" . $item_link
  . "'>" . $item_title . "</a>");
  echo ("<br />");
  echo ($item_desc . "</p>");
  }

 

HTML file:

 

<html>
<head>
<script type="text/javascript">
function showRSS(str)
{
if (str.length==0)
  { 
  document.getElementById("rssOutput").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("rssOutput").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getrss.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body onload="showRSS('RSS')">
<br />
<div id="rssOutput">RSS-feed will be listed here...</div>
</body>
</html>

Posted

Thanks for your reply! I tried the code(s) above and here is what happened...

 

The HTML file doesn't display my feed staffsites.iceitsupport.net/wheeler_jonathon/index.html

 

and I think it's because PHP returns these errors staffsites.iceitsupport.net/wheeler_jonathon/rss.php

Warning: DOMDocument::load() [domdocument.load]: Empty string supplied as input in /home/iceitsup/public_html/Other/StaffSites/wheeler_jonathon/rss.php on line 12

 

Fatal error: Call to a member function getElementsByTagName() on a non-object in /home/iceitsup/public_html/Other/StaffSites/wheeler_jonathon/rss.php on line 16

Posted

More errors

Warning: DOMDocument::load() [domdocument.load]: Couldn't resolve host name in /home/iceitsup/public_html/Other/StaffSites/wheeler_jonathon/rss.php on line 12

 

Warning: DOMDocument::load(http://news.iceitsupport.net/feeds/posts/default) [domdocument.load]: failed to open stream: operation failed in /home/iceitsup/public_html/Other/StaffSites/wheeler_jonathon/rss.php on line 12

 

Warning: DOMDocument::load() [domdocument.load]: I/O warning : failed to load external entity "http://news.iceitsupport.net/feeds/posts/default" in /home/iceitsup/public_html/Other/StaffSites/wheeler_jonathon/rss.php on line 12

 

Fatal error: Call to a member function getElementsByTagName() on a non-object in /home/iceitsup/public_html/Other/StaffSites/wheeler_jonathon/rss.php on line 16

Guest Geoff
Posted

Looks like it's working now.

Posted

Oops! Sorry about that! I found a less complicated script that ran for a while then it started giving errors as well. I then cleared my cache and it works for now.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...