Starbeamrainbowlabs

atom.gen.php - A Simple PHP Atom Feed Generator

What it is and what it does

atom.gen.php is a PHP based Atom feed generator designed to be simple and easy to use, so long as you know how to use PHP classes. Everything is packaged into one neat class in one file. Simply download the file and require() it and you are ready to start using it.

Examples

Basic Example

<?php

//require the real atom.gen.php (the one located at /code/phpatomgenerater/atom.gen.php only outputs the source code)
require("../../../blog/atom.gen.php");

$myfeed = new atomfeed(); //create a new feed

$myfeed->title = "My Awesome feed";					//set the title of the feed
$myfeed->subtitle = "An atom.gen.php example";		//set the subtitle of the feed - optional
$myfeed->id_uri = "https://example.com/blog/";		//set the location of the feed's content
$myfeed->feed_uri = "https://example.com/blog/feed.php"; //set the location of the atom feed itself

//a url that points to a small square icon representing the feed - optional
$myfeed->sq_icon_uri = "https://example.com/images/feedicon.png";
//a url that points to larger icon representing the feed - optional
$myfeed->logo_uri = "https://example.com/images/feed.png";

//add some categories
//simply pass this function an array of categories and they will be added to the feed
$myfeed->addcategories(["travel", "holidays"]);		

//you can even call this function more than once - no categories will be overwritten.
$myfeed->addcategories(["tours"]);		

//add the feed's authors
//format: $feed->addauthor(<name>, <email>, <uri>, <type>);
//the name is the only requirement. To skip a parameter, set it to false.
//the <type> can either be "author" (the default), or "contributor".
$myfeed->addauthor("Bob", "[email protected]", "https://bob.com/", "author");
$myfeed->addauthor("John", "[email protected]", false, "author");

$myfeed->addauthor("Bill", "[email protected]", "https://bill.com/", "contributor");



//format: $myfeed->addentry(<uri>, <title>, <updated>, <authorname>, <content>, <summary>, <categories>, <published>, <rights>);

/*
 * Format Explanation
 * ==================
 * <uri>		| The uri that corresponsed to the content of the entry
 * <title>		| The title of the entry
 * <updated>	| The timestamp the entry was last updated
 * <authorname>	| The author's name. All the other details about the author / contributor are filled in from the authors added through $feed->addauthor();.
 * <content>	| The content of the entry. May contain HTML.
 * <summary>	| A summary of the entry. May contain HTML.
 * <categories>	| Categories / tags that correspond to the entry
 * <published>	| The timestamp the entry was first published. 
 * <rights>		| The license text. Defaults to the global license (which in turn defaults to CC-BY-SA). May contain HTML.
 * 
 * Only the last 4 options are optional.
 */

$myfeed->addentry("https://example.com/oranges", "Oranges", 1407064828, "Bob", "Insert text here");
$myfeed->addentry("https://example.com/apples", "Apples", 1407068828, "Bill", "Insert some text here", "Insert summary here");

////////////////////////////////////////////////////////////

//set the content-type
header("content-type: application/atom+xml");

//render and output the generated feed
echo($myfeed->render());

?>

The output of the above script can be found here.

Reference

After creating a new atomfeed instance, the following properties and function will be available for you to use:

Name Type Use
title property The title of the Atom feed.
subtitle property The subtitle of the Atom feed.
id_uri property A uri that points to the feed's content. For example, if a feed was created for a blog, this property would be set to the blog's homepage.
feed_uri property The uri at which the feed can be found. Simply set it to the uri at which one can find the atom feed on your webserver.
xsltransform_uri property The uri at which an xslt transformation stylesheet can be found. Defaults to false. If set an appropriate declaration is made at the top of the rendered output pointing to the indicated uri.
usecdata property Whether the content of <content> tags should be enclosed in <![CDATA[...]]> tags.
sq_icon_uri property Optional. The uri at which one can find a small square icon that corresponds to the feed.
logo_uri property Optional. The uri at which one can find a larger logo in the ratio 2:1 that corresponsed to the feed.
rights property Optional - Defaults to "CC-BY-SA". May contain HTML. The global license / rights text.
addcategories($categories = []) function Optional. Add an array of categories to the feed.
addauthor($name, $email = false, $uri = false, $type = "author") function Adds an author or contributor to the feed. The authors and contributors added here are used when author's names are specified when adding entries to the feed via addentry(). All parameters apart from $name are optional. To add a contributor instead of an author (the default), simply set the $type to "contributor" instead of author.
addentry($uri, $title, $updated, $authorname, $content, $summary = false, $categories = [], $published = false, $rights = false) function Adds a new entry to the feed. See the basic example above for an explanation.
render() function Renders the atom feed and returns the resulting XML in a string that is ready to be sent to the client.

Several additional functions and constants are also available, but it is advised that you do not use them as they are for internal use only.

I found a bug / want to add a feature!

You can report bugs or request features in the comments on this blog post. You can also fix the bug or add the feature yourself and post a comment on the aforementioned blog post (make sure to link to the modified version of atom.gen.php), and your changes may be accepted into the master version of atom.gen.php. You will be given full credit in the comment at the top of the file if this is the case.

Download

Simply download the atom.gen.php PHP script and upload it to your web server, and you are ready to start. No write permissions are needed in order to use this script.

Download


Art by Mythdael