How to Read an RSS Feed Using PHP 


               


How to Read an RSS Feed Using PHP

PHP







In this article, we show how to read an RSS feed using PHP.

So RSS feeds are a way that a website has of sharing information that can be used by the rest of the internet to see what new articles they have written.

RSS stands for Really Simple Syndication.

If you look up the definition for syndication, it means the supply or broadcast content.

When a website creates an RSS feed, it puts the contents of the new articles that it publishes on the RSS feed, such as the article title, the link to the article, the publication date, and so on.

Users who are subscribed to the RSS feed then get fed this new updated content. So a user doesn't have to visit the site to see if there any new articles. Instead, through subscription to the RSS feed, a user can automatically know, through the RSS feed, when there are new articles.

Besides subscribiing to an RSS feed, another way to do it is to create a PHP program that reads an RSS feed.

This is what we'll do in this article.

PHP Code

The PHP code to search an RSS feed for any keyword is shown below.



So the $rss variable simulates a DomDocument.

The $rss variable loads the CNN tech RSS feed.

This is the RSS feed for the technology section of the CNN website.

We then create an array named $list.

This will hold all of the items in the RSS feed (an item represents an article on that website).

So next in our code, we have a loop. We loop through each item in the RSS feed for a given site through the foreach loop. This foreach loop takes the $rss variables which holds represents the entire RSS feed as a document object model.

The array $list will be an associative array, that is an array composed of name/value pairs or property/value pairs. We have 4 names or properties in this associative array: title, descript, link, and date. We assign these respectively to the title of the article, the description of the article, the hyperlin to the article, and the date the article was published.

We get the values of these tags through the getElementsByTagName() function. Inside of this function, we pass the tag that we want to retrieve.

For each item in the RSS feed, we add it to the array $list via the array_push() function. This portion of the code ends when we've gone through each item in the RSS feed.

Next, we create a variable named $numberofresults. This is the number of results we show from the RSS feed. We limit it to this number.

We create a for loop just for the purpose of us being able to loop through each element that was pushed onto the array we've created, $list.

We start at the first element (element 0) and go until we have gotten 10 results.

After this, we echo out as a header (an h1 tag) CNN Articles. This is because the articles are from CNN.

We then create a PHP variable $title, which we set equal to $list[$i]['title']. This gets the title from the $list array.

We then create a variable named $link, which we set equal to $list[$i]['link']. This retrieves the link from the array.

We do the same thing for $description.

For the $date variable, we get the date by using the strtotime() function to convert the string in the XML RSS feed to a time value that can be formatted in PHP.

Now we go on to outputting the values through the echo() function. We output the title, link, date, and description.

And this is all that is required to read an RSS feed using PHP.

Running the code above gives us the output seen at the following link: RSS Feed of CNN Tech.


Related Resources

How to Search an RSS Feed By Keywords Using PHP




HTML Comment Box is loading comments...