Papermashup

Subscribe


Tweets


"Is there ever a time that @DFS doesn't have a sale on?"

@ashleyford 3 days ago

"RT @lalpra: Galaxy Note 7 Unboxing https://t.co/FM8gZmqpBK"

@ashleyford 5 days ago

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Build an API

AshleyAshley

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.


';
?>




	My Feed Title
	http://papermashup.com
	A description of your content
	en
	
query($query);
while($row = $connector->fetchArray($result)){
	$pubDate = $row['date'];
	$message = str_replace("<", "<", $row['content']);
	
?>
	
		<?php echo stripslashes($message);?>
		http://www.papermashup.com
		
		]]>
	



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.

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Comments 5
  • backlinks
    Posted on

    backlinks backlinks

    Reply Author

    It’s in point of fact a great and useful piece of info. I’m glad that you just shared this helpful info with us. Please keep us up to date like this. Thanks for sharing.


  • Salnazi
    Posted on

    Salnazi Salnazi

    Reply Author

    nice…!


  • Jiji
    Posted on

    Jiji Jiji

    Reply Author

    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.


  • Neil
    Posted on

    Neil Neil

    Reply Author

    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
      Posted on

      Ashley Ashley

      Reply 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 :)