This article will cover how you can create a very simple API (Application Programming Interface) for any one of your projects. We are going to be using PHP and MySQL for the back end, and we will output our API data in two formats: XML or JSON.


API stand for Application Programming Interface. Put simply, it is a way for everyone (if you so choose) to access your website’s data. For example, let’s say that you have a website where users submit games they like. There is a lot of content that is associated to the initial submission, and you want all of this to be more accessible. This is a great example of a place where an API can do wonders. Take Twitter as an example, their success can be attributed largely to the success of their open API, providing developers with the “fire hose” to make their own apps and drive Twitter further.


Database:
For the purposes of this article, we will be using the situation that I mentioned above: you own a game database and you want those to be accessible to a larger audience.
We will accomplish this by providing this data in our API:

   1.  Game ID
   2.  Game Name
   3. Game Poster
   4. Game Info
   5. Game Link

Code steps:
I recommend you use the following URL structure (or something similar):  http://api.example.com/game/

Once you have this structure, create a new file in the "game" folder called "api.php" of sarver script. This is the main file that will be requested by developers after our data. Add the following to this file:

Code of api.php:


Code explain:
    First, we need to get some data from the user: What format do you want? How many responses do you want?

    These are defined in the GET variables format and num. To start off our script, we check if these values are even supplied; if they’re not, the script won’t do anything. If they are both supplied, we move on by connecting to our database, running a relatively simple query to get all the rows, order them in descending order by their id, and limit the response to how many were requested.

    The majority of our script is taken up by the output processing. After we have our data selected into a variable, we need to get it out to the user. First, we check if the user wanted JSON or XML, and depending on that, we serve up the proper output. Let’s go a little more in depth on each of the output styles…

JSON Output:
This one, luckily, is very simple. All we are doing is putting our data into an array, then using PHP’s very handy json_encode() function to encode the JSON. Here’s what I got with my dummy data outputting JSON:a into an array, then using PHP’s very handy _json_encode_ function to encode the JSON. Here’s what I got with my dummy data outputting JSON like this image...



XML Output:
The XML is a little more tricky, it requires the use of the very powerful (and scary) for() function. Here, we are basically saying this: As long as the variable $i is less than the number of rows that we returned, go ahead and add this to our $output variable. Once you’ve done that, add one to the $i variable. Here’s the output I got like this....




Notes:
There are a few things I have left out of this article for the sake of simplicity. Once you have the basics of this figured out, you can add these yourself. A main thing I left out is the necessity of an API Key. If you want to manage who gets your data how, you must assign them an alphanumeric key of at least 32 characters, this will let you know who is accessing your data and you are also able to cut out just anyone from getting at your site. Another key part left out is the fact that you need to manage this API! Twitter limits their API to 1000 requests per day, so you can get an idea of what you want your API limits to be from that.