Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OPTIONS in PHP REST API

Tags:

rest

php

I'm writing a REST API in PHP and I can't find any resources explaining how to work with OPTIONS. It seems to be an important part of REST API's, that's about how much I've found.

Can anyone explain what OPTIONS should contain and how it should be formatted?

Thanks in advance.

like image 395
Karl Laurentius Roos Avatar asked Nov 23 '10 15:11

Karl Laurentius Roos


People also ask

What are options in REST APIs?

OPTIONS tells you things such as "What methods are allowed for this resource". HEAD gets the HTTP header you would get if you made a GET request, but without the body. This lets the client determine caching information, what content-type would be returned, what status code would be returned.

Is PHP GOOD FOR REST API?

There are many PHP Frameworks that can be used to create REST APIs. Besides what I've mentioned above, there are several other popular PHP frameworks. Laravel, Symfony, CodeIgniter, Yii, CakePHP, and others can all create REST APIs. Depending on your needs, anyone of those frameworks may be best for you.

What are the 4 most common REST API operations?

These operations stand for four possible actions, known as CRUD: Create, Read, Update and Delete. The server sends the data to the client in one of the following formats: HTML. JSON (which is the most common one thanks to its independence of computer languages and accessibility by humans and machines)


2 Answers

The HTTP OPTIONS method returns (at least) the HTTP methods that the server supports for a specific URL. Per example, if you have a resource (/user) where you can create, retrieve and retrieve headers (but not delete or update), OPTIONS should return the following response header:

Allow: GET,HEAD,POST

If you can delete the resource as well, it would be:

Allow: GET,HEAD,POST,DELETE

You get the idea.

like image 87
netcoder Avatar answered Nov 15 '22 13:11

netcoder


See section 9.2 http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

like image 43
bcosca Avatar answered Nov 15 '22 14:11

bcosca