Our clients love having the ability to control the content of their web sites. Now that we've been in the business of creating iOS and Android apps for a bit, we're also finding that our clients love having the ability to control the content of their apps. While the platforms are totally different, we've found that our mobile applications can benefit from integration with our favorite CMS in much the same way as our web sites. Today I'll show you how we use ExpressionEngine to easily provide content to our iOS and Android projects through a simple REST server.
You might think that this is going to be difficult. It's not. Simply shift your thinking from creating templates that display well-written prose inside of a beautiful design, to creating templates that expose various bits of content in a machine-readable format. For this example we'll choose JSON; although, you could easily choose XML, or with a little more work, a tab-delimited format. A really neat side-effect to building your API in ExpressionEngine is that it can coincide---and share content with---your existing web site!
Channels & Custom Fields
Nothing changes here. Set up your channels and custom fields just as you normally would when building a web site. When building out a REST API with ExpressionEngine, I tend to think of my channels as database tables and my custom fields as columns in the table. So if you're wanting to create an API that spits out products, you might have a products channel and each custom field would be attributes of that product.
Remember the shift in thinking I mentioned earlier? Here's where it matters. Your templates won't be outputting HTML code; rather, you'll be outputting JSON code---and it's super simple to setup. Here's an example template from an iOS project I worked on that pulled in Vimeo videos. The client is able to add/remove/change/reorder the videos at any time and the iOS app will download the new/changed content as it become available.
So let's break this down:
- First, I used a handly HTTP header plugin to create the appropriate JSON headers.
- Second, I setup a normal Channel:Entries tag to pull the videos from the channel.
- If no results were found, send to the 404 page. You can use the no_results conditional to output anything you wish. You could even set a new HTTP header.
- Finally, we build the JSON object with the attributes needed in the application.
One thing to note, a trailing comma in a JSON array is not valid. The Channel:Entries' backspace parameter is perfect for this situation. It will remove 8 characters from the final output which will eliminate the trailing comma.
Once we hit that template in our web browser---or our iOS application, the JSON containing our Vimeo video content should be displayed as you would expect. Admittedly, this is a very simple example. APIs that are used in the real world will want a few other features for rate limting, authentication, caching, etc. Through the use of both ExpressionEngine's built-in features and a few custom plugins, you should be able to build a decent API whose content can be managed effortlessly by your clients or staff.Share Article