Demo RSS Feed

Create an RSS feed with PHP

First create a new php file called feed.php it needs to end in .php as php will be required in the file. As RSS feeds use XML the browser needs to be told to expect XML rather then php/html.

header('Content-type: application/xml');

Next connect to the database, change the username, pasword and database to match your own.

define('DB_SERVER', 'localhost'); define('DB_USER', 'username'); define('DB_PASSWORD', 'password'); define('DB_NAME', 'database'); $conn = new PDO("mysql:host=".DB_SERVER.";port=8889;dbname language-php">echo "\n"; echo "\n"; echo "\n"; echo "RSS Description\n"; echo "http://www.mydomain.com\n";

For the feed items loop through the database rows, in this case I’m using a news table.

$stmt = $conn->query('SELECT * FROM news ORDER BY newsDate DESC LIMIT 10'); while($row = $stmt->fetch(PDO::FETCH_OBJ)) 

The feed items are stored in an item tag inside the item the following tags can be used:

The following is the items in the loop, the date needs to be formatted to be a valid date formate for the RSS feed.

 echo "\n"; echo "\n"; echo "$row->newsDesc\n"; echo "".date('D, d M Y H:i:s',strtotime($row->newsDate))." GMT\n"; echo "http://www.mydomain.com/$row->newsSlug\n"; echo "http://www.mydomain.com/$row->newsSlug\n"; echo "newsSlug' rel='self' type='application/rss+xml'/>\n" echo "\n";

After the loop close the channel and rss

Putting it all together:

setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "\n"; echo "\n"; echo "\n"; echo "RSS Description\n"; echo "http://www.mydomain.com\n"; $stmt = $conn->query('SELECT * FROM news ORDER BY newsDate DESC LIMIT 10'); while($row = $stmt->fetch(PDO::FETCH_OBJ)) < echo "n"; echo "\n"; echo "$row->newsDesc\n"; echo "".date('D, d M Y H:i:s',strtotime($row->newsDate))." GMT\n"; echo "http://www.mydomain.com/$row->newsSlug\n"; echo "http://www.mydomain.com/$row->newsSlug\n"; echo "newsSlug' rel='self' type='application/rss+xml'/>\n"; echo "\n"; > echo "\n"; echo "\n"; ?> 

Extra Options

Images can be displayed in the description by using a CDATA tag followed by the image source, used with the description go inside the CDATA:

Источник

Php content type xml rss

Thanks for this great tutorial. Just one thing. Shouldn't be all the major element info about RSS hanged on channel instead of the rss element? If I do this like you're presenting, there would be and nothing else. 🙂

Firstly, many thanks for this script. I had been using a mysql script and it would not work when I converted it to mysqli. So this was a great place to start.Skynet is right. This script was closing the Channel tag right at the top, without an open one. This is my amended code (including an image and categories).

 $mysql_host = 'localhost'; //host $mysql_username = ''; //username $mysql_password = ''; //password $mysql_database = ''; //db date_default_timezone_set('Europe/London'); //set your SERVER timezone header('Content-Type: text/xml; charset=utf-8', true); //set document header content type to be XML $rss = new SimpleXMLElement(''); $rss->addAttribute('version', '2.0'); $channel = $rss->addChild('channel'); //add channel node $category = $channel->addChild('category', 'Health'); //add category node $category = $channel->addChild('category', 'Nursing'); //add category node $category = $channel->addChild('category', 'Care Homes'); //add category node $category = $channel->addChild('category', 'etc. etc. etc.'); //add category node $atom = $channel->addChild('atom:atom:link'); //add atom node $atom->addAttribute('href', 'URL to your page. '); //add atom node attribute $atom->addAttribute('rel', 'self'); $atom->addAttribute('type', 'application/rss+xml'); $title = $channel->addChild('title','Your title here. '); //title of the feed $description = $channel->addChild('description','Your description here. '); //feed description $link = $channel->addChild('link','URL to your page. '); //feed site $language = $channel->addChild('language','en-gb'); //language $atom = $channel->addChild('image'); //add an image (logo) $atom->addAttribute('title', 'Your image title here. '); $atom->addAttribute('url', 'Link to your image here. '); $atom->addAttribute('link', 'Your main domain name here. '); $atom->addAttribute('width', 'xxx'); //just numbers $atom->addAttribute('height', 'xxx'); //just numbers //Create RFC822 Date format to comply with RFC822 $date_f = date("D, d M Y H:i:s T", time()); $build_date = gmdate(DATE_RFC2822, strtotime($date_f)); $lastBuildDate = $channel->addChild('lastBuildDate',$date_f); //feed last build date $generator = $channel->addChild('generator','PHP Simple XML'); //add generator node //connect to MySQL - mysqli(HOST, USERNAME, PASSWORD, DATABASE); $mysqli = new mysqli($mysql_host, $mysql_username, $mysql_password, $mysql_database); //Output any connection error if ($mysqli->connect_error) < die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error); > $results = $mysqli->query("SELECT **YOUR STUFF** [I selected Title, NURL, RSSDesc, NDate] FROM TABLE"); if($results)< //we have records while($row = $results->fetch_object()) < $item = $channel->addChild('item'); //add item node $title = $item->addChild('title', $row->Title); //add title node under item $link = $item->addChild('link', 'URL to your page. /'.$row->NURL.'/'); //add link node under item $guid = $item->addChild('guid', 'URL to your page. /'.$row->NURL.'/'); //add guid node under item $guid->addAttribute('isPermaLink', 'true'); //add guid node attribute - true or false $description = $item->addChild('description', $row->RSSDesc); //add description $date_rfc = gmdate(DATE_RFC2822, strtotime($row->NDate)); $item = $item->addChild('pubDate', $date_rfc); //add pubDate node > > echo $rss->asXML(); //output XML

Источник

simple rss feed

i am trying to make a simple rss feed but the problem is when i run the file is says would you like to open rss.php . here is the code maybe im doing something wrong? i have put it in this format just to see it work.

 <?xml version='1.0' encoding='ISO-8859-1'?>   this is my example http://localhost:8888/redline Copyright (C) 2010 sarmenhb  This is the description of the first example. http://www.example.com/example1.html Mon, 29 Dec 2008 22:10:00 -0600   This is the description of the first example. http://www.example.com/example1.html Mon, 29 Dec 2008 22:10:00 -0600   This is the description of the first example. http://www.example.com/example1.html Mon, 29 Dec 2008 22:10:00 -0600   This is the description of the first example. http://www.example.com/example1.html Mon, 29 Dec 2008 22:10:00 -0600   This is the description of the first example. http://www.example.com/example1.html Mon, 29 Dec 2008 22:10:00 -0600    

2 Answers 2

You should output real , not an entitized version. Since it's problematic due to php's own

'; ?>   this is my example http://localhost:8888/redline Copyright (C) 2010 sarmenhb  This is the description of the first example. http://www.example.com/example1.html Mon, 29 Dec 2008 22:10:00 -0600   This is the description of the first example. http://www.example.com/example1.html Mon, 29 Dec 2008 22:10:00 -0600   This is the description of the first example. http://www.example.com/example1.html Mon, 29 Dec 2008 22:10:00 -0600   This is the description of the first example. http://www.example.com/example1.html Mon, 29 Dec 2008 22:10:00 -0600   This is the description of the first example. http://www.example.com/example1.html Mon, 29 Dec 2008 22:10:00 -0600    

Источник

Php content type xml rss

RSS makes it possible to syndicate latest website contents throughout the web seamlessly, whether you have a news website, blog or a normal website, you can break it down into discrete items and syndicated them via RSS. If you are using WordPress or any other popular CMS, you will find its own RSS system. But creating your custom RSS feed is also fairly easy using PHP. Let's get started! I will be using PHP DOMDocument class to construct the RSS document. In most circumstances, you want to fetch the records from database and turn the contents into RSS feed, we will be doing exactly that.Create a table in MySQL database using phpMyAdmin console, and insert few rows.

 CREATE TABLE IF NOT EXISTS `site_contents` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(60) NOT NULL, `content` text NOT NULL, `published` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`) );

Once the database is ready, we can write our XML generator code using PHP DOMDocument. First we start off by entering MySql database information, and then set the document header content type to XML, so that browsers understand it as XML document and render appropriately.

The second line in the code below creates a new DOMDocument object, this object lets us build XML document using createElement, appendChild etc. The example creates XML document with node called "RSS".

 createElement("rss"); $rss_node = $xml->appendChild($rss); //add RSS element to XML node $rss_node->setAttribute("version","2.0"); //set RSS version echo $xml->saveXML(); //output: // // ?>

So basically we build XML document using createElement(), setAttribute(), appendChild() and saveXML(). It sounds really easy, but it can get bit complicated as you start adding nodes. Here's the final code that generates RSS document for your website.

 createElement("rss"); $rss_node = $xml->appendChild($rss); //add RSS element to XML node $rss_node->setAttribute("version","2.0"); //set RSS version //set attributes $rss_node->setAttribute("xmlns:dc","http://purl.org/dc/elements/1.1/"); //xmlns:dc (info http://j.mp/1mHIl8e ) $rss_node->setAttribute("xmlns:content","http://purl.org/rss/1.0/modules/content/"); //xmlns:content (info http://j.mp/1og3n2W) $rss_node->setAttribute("xmlns:atom","http://www.w3.org/2005/Atom");//xmlns:atom (http://j.mp/1tErCYX ) //Create RFC822 Date format to comply with RFC822 $date_f = date("D, d M Y H:i:s T", time()); $build_date = gmdate(DATE_RFC2822, strtotime($date_f)); //create "channel" element under "RSS" element $channel = $xml->createElement("channel"); $channel_node = $rss_node->appendChild($channel); //a feed should contain an atom:link element (info http://j.mp/1nuzqeC) $channel_atom_link = $xml->createElement("atom:link"); $channel_atom_link->setAttribute("href","http://localhost"); //url of the feed $channel_atom_link->setAttribute("rel","self"); $channel_atom_link->setAttribute("type","application/rss+xml"); $channel_node->appendChild($channel_atom_link); //add general elements under "channel" node $channel_node->appendChild($xml->createElement("title", "Sanwebe")); //title $channel_node->appendChild($xml->createElement("description", "description line goes here")); //description $channel_node->appendChild($xml->createElement("link", "http://www.sanwebe.com")); //website link $channel_node->appendChild($xml->createElement("language", "en-us")); //language $channel_node->appendChild($xml->createElement("lastBuildDate", $build_date)); //last build date $channel_node->appendChild($xml->createElement("generator", "PHP DOMDocument")); //generator //Fetch records from the database //connect to MySQL - mysqli(HOST, USERNAME, PASSWORD, DATABASE); $mysqli = new mysqli($mysql_host, $mysql_username, $mysql_password, $mysql_database); //Output any connection error if ($mysqli->connect_error) < die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error); > //MySQL query, we pull records from site_contents table $results = $mysqli->query("SELECT id, title, content, published FROM site_contents"); if($results)< //we have records while($row = $results->fetch_object()) //loop through each row < $item_node = $channel_node->appendChild($xml->createElement("item")); //create a new node called "item" $title_node = $item_node->appendChild($xml->createElement("title", $row->title)); //Add Title under "item" $link_node = $item_node->appendChild($xml->createElement("link", "http://www.your-site.com/link/goes/here/")); //add link node under "item" //Unique identifier for the item (GUID) $guid_link = $xml->createElement("guid", "http://www.your-site.com/link/goes/here/". $row->id); $guid_link->setAttribute("isPermaLink","false"); $guid_node = $item_node->appendChild($guid_link); //create "description" node under "item" $description_node = $item_node->appendChild($xml->createElement("description")); //fill description node with CDATA content $description_contents = $xml->createCDATASection(htmlentities($row->content)); $description_node->appendChild($description_contents); //Published date $date_rfc = gmdate(DATE_RFC2822, strtotime($row->published)); $pub_date = $xml->createElement("pubDate", $date_rfc); $pub_date_node = $item_node->appendChild($pub_date); > > echo $xml->saveXML(); ?>

I have created sample file, which can be downloaded from this page. I suggest that you test and play with the example file to understand how we use PHP DOMDocument class to create XML documents. If you want to create RSS using PHP SimpleXML functions, here is another example. Download

Источник

Читайте также:  Java driver for access
Оцените статью