A Simple REST API Toolkit

Last week Dave and I were trying to figure out the best way to synchronize member data from an asp application with a WordPress installation. I don’t remember how it came up in conversation, but in a flurry of chatter we decided that we should build a RESTful API to allow the applications in question to communicate. In the interest of re-usability we also decided that we should build a generic toolkit. One that could easily be used to expose an API for any supported database, through PHP. So, using this toolkit you should quite easily get your Lasso application talking with your PunBB software, if that’s what you need.

So was born rest-toolkit. We have released an alpha version of the project, so download it and give it a try. I hope you don’t mind a lack of formal documentation. Depending on how things go documentation will follow.

The quick install guide

  • Find a PHP project you have in the works that talks with a database
  • Make sure you don’t care about the data in the database, especially if you’re going to be using DELETE
  • Unzip the rest-toolkit into a directory (perhaps /api)
  • Open config/config.php and make any changes that need to be made
  • Modify the database connection information in database/mysqli.php. If you want to use regular old mysql you’ll have to make some additions to the mysql.php file (take a look at the iDatabase interface file for some clues)
  • Do you have a users table in your database? If so, visit http://your.url/restapidir/users/, or http://your.url/restapidir/users.xml, or http://your.url/restapidir/users.json, or http://your.url/restapidir/users/1.xml, or http://your.url/restapidir/users/1.json.
  • Take a look at the class.Blocks.php example file for an example of an extended resource file.
  • Of course, before using this in anything even remotely resembling production you would need to protect your API directory and require authentication to access the REST resources.  That part is left up to you.

Some Highlights

  • When you make your REST request the query will be built dynamically based on the field names you send with the request. So, 95% of the work is already done for you.
  • Include an xml or json extension with your request to modify the response type. Need something other than xml or json? You can easily add other extensions and mime-types.
  • Want a clean url, but your clean resource name doesn’t nicely match a database table? Don’t worry, you can extend the resource base class with a class for your clean resource name (for example, class.Users.php), and map the resource to any table in your database. By default the resource name you include in your url will map to a table of the same name.
  • Need to limit what is returned/updated/created in a query? These items are easily configurable in your extended class.
  • Need to limit the resources that your API can talk with? In the resource base class you can define the resources the API should expect to receive requests for.

NOTE: If you do give the toolkit a try please comment out line 335 of class.Resource.php. I neglected to comment that line before packaging up the alpha version.

Tags: , , ,

Leave a Reply