The issue is this:
I have a web application that runs on a PHP server. I’d like to build a REST api for it.
I did some research and I figured out that REST api uses HTTP methods (GET, POST…) for certain URI’s with an authentication key (not necessarily) and the information is presented back as a HTTP response with the info as XML or JSON (I’d rather JSON).
My question is:
- How do I, as the developer of the app, build those URI’s? Do I need to write a PHP code at that URI?
- How do I build the JSON objects to return as a response?
Here is a very simply example in simple php.
There are 2 files client.php & api.php. I put both files on the same url :
http://localhost:8888/, so you will have to change the link to your own url. (the file can be on two different servers).This is just an example, it’s very quick and dirty, plus it has been a long time since I’ve done php. But this is the idea of an api.
client.php
api.php
I didn’t make any call to the database for this example, but normally that is what you should do. You should also replace the “file_get_contents” function by “curl”.