Build an API

5 May 2009| 2 Comments| Print

If you type in a URL to a web site in your browser and that URL returns data in a structured format, then a basic API is already in place. For example, when you access an RSS feed, you’re essentially using an API. You type in a structured URL “asking” the server for information in RSS format and the server (usually) spits out structured data ready for your feed reader to parse.

To take this to the next level, you’ll also want to allow developers to perform actions against the data. If you want to allow someone to delete or modify an entry from your web service, you could have them access a semantically structured URL like http://mysite.com/api/modify/.

If there’s no need to authenticate your users/developers with an API key, a GET request very well might be your method of choice and we can have the users query everything using just the URL alone. For example, selecting entry 3 from a public record could be as easy as typing in the URL: http://site.com/api/select/3.

However, when a lot of parameters need to be set (which would make the URL extremely long), or when security is of greater concern, a POST request is a safer and morecommon approach to asking data from a server. A POST request will usually require some sort of data to go along with the URL parameters so the server can check to make sure the user/developer has permission to do the type of request they’re attempting. This POST data usually contains either a password or a key to authenticate the request.

An easy way to allow developers to interact with your web service is to offer RSS feeds (which is an XML file that’s styled) XML is a standard format that allows developers that code in different languages to serve their data in a format that can be read by parsed by any language. PHP5 includes the SimpleXML element which is a built in function allowing you to parse XML or RSS quickly with minimal code.

Here’s a piece of code PHP that allows you to generate an RSS feed from data from a database.

<?php
header("Content-Type: application/xml; charset=ISO-8859-1");
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>

<rss version="2.0">

<channel>
	<title>My Feed Title</title>
	<link>http://papermashup.com</link>
	<description>A description of your content</description>
	<language>en</language>
	<pubDate><?php echo date("D, d M Y H:i:s T");?></pubDate>
<?php

include("../includes/dbConnector.php");
$connector = new DbConnector();

$query = "SELECT * FROM mytable ORDER BY ID DESC LIMIT 25";
$result = $connector->query($query);
while($row = $connector->fetchArray($result)){
	$pubDate = $row['date'];
	$message = str_replace("<", "<", $row['content']);

?>
	<item>
		<title><?php echo stripslashes($message);?></title>
		<link>http://www.papermashup.com</link>
		<pubDate><?php echo $pubDate;?></pubDate>
		<description><![CDATA[<?php echo stripslashes($message);?>]]></description>
	</item>
<?
};
?>
</channel>
</rss>

You would need to modify the database connection and query to suit your needs as well as modifying the XML output tags, It’s a quick and dirty way of making your content accessible.

2 Comments

  • Neil

    It would be more like an API if the query was dynamic – i.e adding where, and and between clauses that get set from URL params

  • Ashley (author)

    @neil yeah totally, this is just a simple introduction on how you can deliver content that’s accessible to developers using any language using XML. I’ll do a tutorial about building a dynamic API in the future, thanks for the feedback :)

Leave a comment...

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled site. To get your own globally-recognized-avatar, register at Gravatar.