Page 1 of 1

code in php for rss feed from any news site

Posted: Mon Jul 20, 2015 4:59 am
by anjshr7
Can any one tell me the code in php for rss feed from any news site .....

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

Posted: Mon Jul 20, 2015 12:40 pm
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.

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

Posted: Fri Jul 24, 2015 4:47 pm
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>";