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?
In 2013, you should use something like Silex or Slim
Silex example:
Slim example:
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".
As simon marc said, the process is much the same as it is for you or I browsing a website. If you are comfortable with using the Zend framework, there are some easy to follow tutorials to that make life quite easy to set things up. The hardest part of building a restful api is the design of the it, and making it truly restful, think CRUD in database terms.
It could be that you really want an xmlrpc interface or something else similar. What do you want this interface to allow you to do?
--EDIT
Here is where I got started with restful api and Zend Framework. Zend Framework Example
In short don't use Zend rest server, it's obsolete.
That is pretty much the same as created a normal website.
Normal pattern for a php website is:
With a api, you just add a new step between 3 and 4. After 3, create a array with all information you need. Encode this array in json and exit or return this value.
That all for the api. For the client side, you can call the api by the url. If the api work only with get call, I think it's possible to do a simply (To check, I normally use curl).
But it's more common to use the curl library to perform get and post call. You can ask me if you need help with curl.
Once the get the info from the api, you can do the 4 & 5 steps.
Look the php doc for json function and file_get_contents.
curl : http://fr.php.net/manual/fr/ref.curl.php
EDIT
No, wait, I don't get it. "php API page" what do you mean by that ?
The api is only the creation/recuperation of your project. You NEVER send directly the html result (if you're making a website) throw a api. You call the api with the url, the api return information, you use this information to create the final result.
ex: you want to write a html page who say hello xxx. But to get the name of the user, you have to get the info from the api.
So let's say your api have a function who have user_id as argument and return the name of this user (let's say getUserNameById(user_id)), and you call this function only on a url like your/api/ulr/getUser/id.
From the client side you do
So the client never access directly the databases, that the api's role.
Is that clearer ?
I know that this question is accepted and has a bit of age but this might be helpful for some people who still find it relevant. Although the outcome is not a full RESTful API the API Builder mini lib for PHP allows you to easily transform MySQL databases into web accessible JSON APIs.
There is no standard for how an API URI scheme should be set up, but it's common to have slash-separated values. For this you can use...
...to get an array of slash-separated values in the URI after the file name.
Example: Assuming you have an API file
api.php
in your application somewhere and you do a request forapi.php/members/3
, then$apiArgArray
will be an array containing['members', '3']
. You can then use those values to query your database or do other processing.You can take any PHP object and turn it into JSON with json_encode. You'll also want to set the appropriate header.
All this is good for an API that returns JSON, but the next question you should ask is:
(3) How do I make my API RESTful?
For that we'll use
$_SERVER['REQUEST_METHOD']
to get the method being used, and then do different things based on that. So the final result is something like...Sources: https://stackoverflow.com/a/897311/1766230 and http://en.wikipedia.org/wiki/Representational_state_transfer#Applied_to_web_services