Build an API

Build an API

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on May 5, 2009

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.


Recent shares

More tutorials from Papermashup
Comments
4 discussions around Build an API
  1. Jiji says:

    Hi Ashely,

    I’m familiar with PHP, Javascript and Mysql.

    But i didnt familiar with an API building, i used in java like import functionality which is used to import different Modules.

    Could you please explain who to make API in PHP from scratch.

  2. Neil says:

    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 says:

      @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 Reply

Your email address will not be published. Required fields are marked *

*

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

Looking for a registry cleaner to speed up your PC and show a full diagnostics?
Faster surfing with Dish Network High Speed Internet

Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.
Subscribe
OR

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.
Subscribe

Get in contact

Please use the form below to get in touch.

About Me

I'm Ashley Ford, Co-founder and Technical Director at Harkable.com London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.



What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on Papermashup.com you can find details here Or use the contact link below for further advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.