So, I was looking through some articles on creating REST API's.
And some of them suggest using all types of HTTP requests: like PUT
DELETE
POST
GET
.
We would create for example index.php and write API this way:
$method = $_SERVER['REQUEST_METHOD'];
$request = split("/", substr(@$_SERVER['PATH_INFO'], 1));
switch ($method) {
case 'PUT':
....some put action....
break;
case 'POST':
....some post action....
break;
case 'GET':
....some get action....
break;
case 'DELETE':
....some delete action....
break;
}
OK, granted - I don't know much about web services (yet).
But, wouldn't it be easier to just accept JSON object through regular POST
or GET
(that would contain method name and all parameters) and then respond in JSON as well. We can easily serialize/deserialize via PHP's json_encode()
and json_decode()
and do whatever we want with that data without having to deal with different HTTP request methods.
Am I missing something?
UPDATE 1:
Ok - after digging through various API's and learning a lot about XML-RPC, JSON-RPC, SOAP, REST I came to a conclusion that this type of API is sound. Actually stack exchange is pretty much using this approach on their sites and I do think that these people know what they are doing Stack Exchange API.
In regards to using extension to define data type. I noticed that MailChimp API is doing it, but I don't think this is a good idea.
My sound like a good idea, but I think "older" approach is better - using HTTP headers
Also HTTP headers are much better for cross data type communication (if ever someone would need it)
In short, REST emphasizes nouns over verbs. As your API becomes more complex, you add more things, rather than more commands.