HS Banner
Back
Show RSS News Feeds

Author: Admin 05/01/2021
Language: PHP
Tags:


Description:

Here's some code I've used in the past to show RSS news feed on PHP websites.

Article:

Create a getrss.php file with the falling code.

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


//find out which feed was selected
if($q=="Google") {
    $xml=("http://news.google.com/news?ned=us&topic=h&output=rss");
} elseif($q=="ZDN") {
    $xml=("https://www.zdnet.com/news/rss.xml");
}elseif($q=="CNN") {
    $xml=("http://rss.cnn.com/rss/cnn_topstories.rss");
}elseif($q=="NYT") {
    $xml=("https://rss.nytimes.com/services/xml/rss/nyt/US.xml");
}elseif($q=="LAT") {
    $xml=("https://www.latimes.com/local/rss2.0.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>");
}

On the page you want to display the news feeds add this to the header section.

<script>
        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 (this.readyState==4 && this.status==200) {
                    document.getElementById("rssOutput").innerHTML=this.responseText;
                }
            }
            xmlhttp.open("GET","getrss.php?q="+str,true);
            xmlhttp.send();
        }
    </script>

Then add a select box to the same page.

<form>
    <select class="form-select" onchange="showRSS(this.value)">
         <option value="">Select News</option>
         <option value="Google">Google News</option>
         <option value="ZDN">ZDNet News</option>
         <option value="CNN">CNN Top Stories</option>
         <option value="NYT">New York Times</option>
         <option value="LAT">Los Angeles Times</option>
    </select>
</form>
<div id="rssOutput"></div>


Back
Comments
Add Comment
There are no comments yet.