Creating an API in PHP
One of the key components of my GSoC Project was to have a POST API for the Android App generator.
This was required so that the app generator could be plugged into the server and can be called directly instead of someone manually visiting the webpage and entering his/her details.
It takes in a JSON input and compiles and emails the app to the organizer based on his email address in the input JSON.
The input to the API will look something like this :
Once the data is sent, on the server I have a php file which intercepts the requests and performs an action based on the request.
The code above is pretty much self explanatory.
So basically, first we check for a valid request (GET/POST) and throw an error if it is invalid.
Next up, for a valid request we store the body into a variable and then execute a followup script as per our needs using data from this response.
This PHP file should be located in the public-html (/var/www/data) of the server so as to be accessible from outside of the server.
You can test out this API by calling it directly by prepending the server’s ip address to the name of php file containing this code.
Something like :
Well, that’s it then!
You can easily modify the PHP code provided and modify it to suite your needs for making you own API.
Let me know your thoughts and your queries in the “response” ;) below.
Until next time.