{ Lekker Logic }

Technology Solutions

Data API Using Slim Framework and MySQL

One of the best programming tools I discovered recently was a PHP micro-framework called SLIM.  Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.  I am a sucker when it comes to translating data into usable formats for consumption by great user interfaces.  For a current project I am working on the need to develop a custom data API to serve JSON out to a web app service was required.  Admittedly, I was a bit worried at first on how to exactly do this in a quick and efficient method.  Luckily I found Slim and life is good!

CaptureTo start using Slim you will of course need to download and install it either via composer or the zip archive.  For this example I will use the file structure shown here to place my files and create the data API.  You can download the project files here if you want to use my example.  I will discuss the V1 folder and htaccess and index file shortly.  The slim framework is relatively small at 197kb, hence its name “Slim” I suppose?  Not sure but that makes sense to me. Once you have your folder structure in place we can dive into the index.php file which is the heart of what we will be discussing.



Within this file is where I customize the data API.  I don’t have to dive deep into Slim much (at least for this example) other than just making sure its required here.  Also, It should be noted I am using MySQL as the back-end database to store my data points – more on this as we go.  Another note is this example is using the HTTP GET method for all requests.  Slim can be used as a full RESTful API as needed.

To begin we require Slim to use it in our file, get a Slim instance started, set our end-points for use in the data request and finally run the program.

The data end-points are important to understand as this is where your URL will point for your API to request the various data you need.  In this example we will be querying some names in our database.  You can add to the end-points to make as many as needed by creating more $app->get end-points.  To navigate to these you would simply do a request to http://yourdomain.com/v1/names or yourdomain.com/v1/names/search/:query where :query is the actual search you are performing such as “Dennis” or another name.

Next I need to have the function references point to actual functions, such as ‘getNames’.   This is where the real logic happens and were we query MySQL for the data.

Now the /v1/names end-point function will get triggered and the above $sql query will be run to return the data we need.  Using a standard MySQL select we can specify how and what type of information we retrieve.  Once I get the data I use json_encode($names)  to place it into the structure needed. Our returned data structure in JSON looks as follows:

To customize a bit more of the API request, I can add some variables to get specific info from a changing end-point.  This is handy when you need an user to interact with your API to get custom data.

Here I use the same concepts from above but add in a $query variable to search the data for a specific name.  Notice the :query parameter being sent into the SQL request based on the end-point /names/search/:query  Essentially whatever you send to the URL will be placed into the select statement.

The last bit of detail needed is the .htaccess file to resolve the URL request appropriately.  This way you dont get any error 404 when trying to connect to your end-points.  This is fairly simple and only three lines:


You can see Slim is very powerful when creating a data API for an application or other means.  As you expand into a variety of different uses and data sets the options can be vast for how you customize this.  Slim allows for a very easy data API back-end to be created quickly and efficiently.   Best of all Slim is free


You can download the source files and check out the example by going to the below data end-points.  The GitHub repo for this project can be found below as well.


/data_api/v1/names/search/:query  – In this case query for the name Dennis

GitHub Repo – https://github.com/rontarson/LekkerLogic-DataAPI

Lekker Logic
Tags: API Services, CRUD, Database, MySQL, PHP, Programming, RESTful API, Slim Framework, Tutorials