Web API

Have you ever tried to create your own API for your website? Most people say to start with your own PHP or Ruby code and work from there. But, most of your information is already in WordPress. Why start fresh? But, WordPress does not have a way to do a flexible Web API within in it....or does it?

The Web API plugin from Custom Computer Tools is just what you need. With this plugin, you can use all the data already in your WordPress site to create any kind of Web API or even Web Application right inside of WordPress itself. I will show you how to install the plugin and use it to your best advantage.

Installation

Instructions coming. I am trying to get it listed with the WordPress Plugins directory and will give instructions to install it from there.

Basic Usage




Click on the admin menu for creating a new API post page and put the following inside:

echo "

This is PHP hello!

"; ?>

This is HTML hello!

When you view this page, all you see is:

This is PHP hello!

This is HTML hello!

If you look at the source for the page, you will see:

This is PHP hello!

This is HTML hello!

No theme stuff (The numbering is my CodeHighlighter shortcode and has nothing to do with the output of the API page). Just some plain text. But, that is exactly what you want when you are serving out an API. You want to be able to just send the raw data that the user needs. The program that requests it can then format it any way they want. You could use this to serve pages that have a unique look and feel, but does not affect the rest of the site. Or, it can return a JSON list of data (ie: Current items on sale, Most viewed articles, ...) that the requesting program will read in and display in a format that matches their theme or need. Since we are stopping WordPress from producing any output to the requester, we have complete control over what is sent.

Also, notice the code in the API page: it starts as PHP, then the PHP closure code, then HTML. The script is executed as a PHP script, but you can then go down to HTML, and then back into PHP. This gives you complete flexibility to use both to make the request. In the PHP, you have full access to all WordPress functions as well. Therefore, you can easily query for articles, sales items, or what ever you have on your site to send to the requester.

Since you have all of the PHP information, you can also process information sent to you from the requester. Just get it from the $_COOKIE, $_GET, $_POST or $_REQUEST global variables. Just remember to do proper validation on all input from the requester so you do not get your site hacked! With flexibility and power comes more areas for attack as well. Just keep that in mind!

Note: I have added code to keep WordPress from automatically formatting these Web API pages. You can see what I did to stop the automatic formatting in this tutorial Stopping WordPress from auto formating. The only difference is that I only do it for Web API pages and not the other post types. If you find that your code in the Web API pages has added p tags, your theme might have added more formatting code to WordPress. You would need to look into what your theme does to find out how to disable that code. I have seen at least two theme that do that.

Examples

For an example, create an API post called "comments" and place this code in the post:

?>

Hi , here are the top 5 posts ordered by number of comments:

  • ()
  • Sorry, no posts were found.

Once you have saved the post, click "View API" at the top of the page. You will see this:

Hi there, here are the top 5 posts ordered by number of comments:

New Theme Configuration Problems (1)
A new tutorial: The Tutorial Post Type (0)
New Tutorial: Color Wheel App (0)
New Tutorial: Smoothly Scrolling Sidebar (0)
Screen Cast first pass is on the site! (0)

But, it is not showing the name, but the word "there"! In the address bar of your browser, add this to the end of the current address:

?name=richard

When you press enter again, the name will show up in the first sentence.

Hi richard, here are the top 5 posts ordered by number of comments:

New Theme Configuration Problems (1)
A new tutorial: The Tutorial Post Type (0)
New Tutorial: Color Wheel App (0)
New Tutorial: Smoothly Scrolling Sidebar (0)
Screen Cast first pass is on the site! (0)

(Note: The post name and stats is taken from this website. There is not many comments right now!). If you add a comment on a post somewhere in this site, the number count will also increase. Give it a try!

Notice, the code will not print the name received from the requester if it has anything other than letters. This is to protect from possible code injection. Remember, never trust requester given data! Always check it out well!

Conclusion

That is the basics of using this plugin. I will have more tutorials on ways to use the plugin. You can share some of your favorite code in the forum as well. Looking forward to see what you create!