Can you use REST in PHP? If so, how?

I am developing my own PHP library and I would like to name RESTful web services from my API. Can this be done in PHP, and if so, what are the basics of this?

+44
rest php restful-architecture
Oct 27 '09 at 3:32
source share
5 answers

Since REST is the application of the same HTTP protocol methods to the client-server architecture design, and PHP is already so good at handling HTTP protocol requests such as GET and POST. PHP is especially suited to simplify the creation of REST services.

Remember that REST is an application of the same http templates that already exist.

So, if you have an application that does something like:

  • HTML form
  • PHP process
  • HTML output in a table

So, to do this REST, you need:

  • Accept settings from the Internet. This is easy, as you will get options either as get or post ... so this is basically the same.
  • PHP process
  • Output in JSON or XML format . And it's all!

    Very easy.

Now the challenge is to create your API (functions and URLs), which you will create in order to be a friendly programmer.

In this case, I suggest you look at the flickr API , as the example is very convenient for developers, easy to guess and has good documentation.

For more information about the API, see this presentation: How to create a good API and why it matters (Joshua Bloch)

Finally, the RESTful API should also implement the PUT and DELETE methods of the http protocol when it makes sense.

For example, if you had a delete action in your api, the specified service should get a delete method from the http protocol. Instead of the more common thing of sending an action parameter as part of a submit request.

Edit: Replaced “Php by default by default” with “Since REST is an application of the same HTTP protocol methods for designing client-server architectures, and PHP is already so good for handling HTTP protocol requests such as GET and POST. PHP is specially suited to simplify the creation of REST services.

And they also added a final note that you should implement the appropriate PUT or DELETE methods when this action makes sense for your api.

+37
Oct 27 '09 at 3:52
source share

You can see this article and the next steps: http://www.gen-x-design.com/archives/create-a-rest-api-with-php/

Your question is very open, so this tutorial might be the best starting point.

The link above no longer works, so check out this guide:

http://net.tutsplus.com/tutorials/other/a-beginners-introduction-to-http-and-rest/

+17
Oct 27 '09 at 3:50
source share

I developed a class that is a native RAP class of the SoapServer class.

You simply include the RestServer.php file and then use it as follows.

class Hello { public static function sayHello($name) { return "Hello, " . $name; } } $rest = new RestServer(Hello); $rest->handle(); 

Then you can make such calls:

 http://myserver.com/path/to/api?method=sayHello&name=World 

(Note that it does not matter in what order the parameters are in the query string. In addition, the parameter key names as well as the method name are case-insensitive.)

+5
Oct 02 '12 at 19:22
source share

It does not hurt to return to the original source of the REST term and make sure that you understand what this means.

+4
Oct 27 '09 at 3:51
source share

If you are thinking about the client side of things, I would suggest checking out the MATS Sukowski PEST.

You will find the repository on GitHub: https://github.com/educoder/pest

Also check out this thread: PHP REST Clients

Update 2013/12/13:
This is a very lively open source project, Matt Sukowski passed it on to his new guardians this summer because he didn't feel he could save enough time, and since then there have been many, many commits. So Pest is better than ever for using Rest in PHP :)

+4
Jan 23 2018-12-01T00:
source share



All Articles