code in php for rss feed from any news site

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
anjshr7
Forum Newbie
Posts: 19
Joined: Fri Jul 17, 2015 1:28 pm

code in php for rss feed from any news site

Post by anjshr7 »

Can any one tell me the code in php for rss feed from any news site .....
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: code in php for rss feed from any news site

Post by Christopher »

It is just an XML document. Use cURL, fsocketopen, the stream functions or other library to access the URL. Then parse the XML. It is in a standard format.
(#10850)
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: code in php for rss feed from any news site

Post by Pazuzu156 »

Christopher wrote:It is just an XML document. Use cURL, fsocketopen, the stream functions or other library to access the URL. Then parse the XML. It is in a standard format.
Pretty much what was said there. I'd go a step further, and parse it with PHP functions. Since RSS is basically XML, you can use simplexml_load_string to load XML and easily parse it.

Here's an example:

Code: Select all

<?php

$content = file_get_contents("https://twitrss.me/twitter_user_to_rss/?user=pazuzu156");
$xml = simplexml_load_string($content);

$channel = $xml->channel;
$items = $channel->item;

$string = "<pre>RSS Feed\n\n";
foreach($items as $item)
{
	$string .= "Item\n====\n";
	$string .= "Title: {$item->title}\n";
	$string .= "Description: {$item->description}\n";
	$string .= "Published Date: {$item->pubDate}\n";
	$string .= "GUID: {$item->guid}\n";
	$string .= "Link: {$item->link}\n\n";
}

echo $string . "</pre>";
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
Post Reply