I am developing my own PHP Library and I would like to call RESTful web-services from my API. Can this be done in PHP and if so what are the basics in doing so?
-
8+1: offsetting the downvote, this is a reasonable (if vague) question. – Jim Ferrans Oct 27 '09 at 04:22
-
2I think he's asking about handling the PUT and DELETE request methods, so I don't think it is an invalid question. – bart Jan 30 '14 at 10:30
-
1Let the question continue you sticklers; what are you trying to prove here? It has over 43,000 views and is the fifth Google result for PHP + REST. Stop killing what this site stands for - CLUELESS QUESTIONS. – Kyle Bridenstine Jan 09 '17 at 03:54
5 Answers
Since REST is the application of the same methods of the HTTP protocol to the design of client-server architectures and PHP is already so good to handle HTTP protocol requests such as GET and POST. PHP is specially suited to make developing REST services easy.
Remember REST is the application of the same http patterns that already exists.
So if you currently have an application that does something like:
- HTML Form
- PHP Process
- HTML Output in a table
So to make it REST you would need to:
Accept parameters from the web. This is easy since you will receive the parameters either as get or post... so it is basically the same.
PHP process
Output in either JSON or XML. And that is it!
Is pretty easy.
Now the difficult part is to make your API (the functions and URLs) that you will generate to be programmer friendly.
In that case I suggest you look at the flickr API as an example is very developer friendly easy to guess and has good documentation.
For more info on APIs look at this presentation: How to Design a Good API & Why it Matters (Joshua Bloch)
Finally a RESTful API should implement also the PUT and DELETE methods of the http protocol when it makes sense
For example if you had a delete action in your api, said service should receive the delete method from the http protocol. Instead of the more common thing of sending an action parameter as part of a post request.
Edit: Replaced "Php is rest by default" with "Since REST is the application of the same methods of the HTTP protocol to the design of client-server architectures and PHP is already so good to handle HTTP protocol requests such as GET and POST. PHP is specially suited to make developing REST services easy."
And also added the final note that you should implement the appropiate PUT or DELETE methods when that action makes sense for your api.
-
2-1: Sorry, no language is REST by default. For instance in REST a POST request is very different than GET, and you need to support DELETE and PUT as well. – Jim Ferrans Oct 27 '09 at 04:20
-
-
@elviejo - If you look at the urls for stackoverflow you will see a REST url. This is not something that PHP does by default, in the parsing, as there is no question mark. – James Black Oct 27 '09 at 04:22
-
10@James Black the fact that StackOverflow makes pretty url rewrites doesn't make it more or less REST. so.com/questions/1628628 is a valid REST call as is this: so.com/questions.php?id=1628628 Both calls are using the get method of the http protocol. – elviejo79 Oct 27 '09 at 07:07
-
1@Jim Ferrans: Added some clarifications in my answer trying to address your concerns. – elviejo79 Oct 27 '09 at 07:30
You may want to look at this article and the follow-up: http://www.gen-x-design.com/archives/create-a-rest-api-with-php/
Your question is very open-ended, so this tutorial may be the best starting point.
The link above is no longer working so check out this tutorial:
http://net.tutsplus.com/tutorials/other/a-beginners-introduction-to-http-and-rest/

- 41,583
- 10
- 86
- 166
I developed a class that is the PHP native SoapServer class' REST equivalent.
You just 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 calls like this:
http://myserver.com/path/to/api?method=sayHello&name=World
(Note that it doesn't matter what order the params are provided in the query string. Also, the param key names as well as the method name are case-insensitive.)

- 32,620
- 21
- 85
- 124

- 4,977
- 12
- 39
- 53
-
-
I didn't have the need for it to in the app I originally used it for. Feel free to fork it and improve upon it. – Jake Sankey Feb 13 '14 at 04:57
Can't hurt to go back to the original source of the term REST, and be sure that you understand what that means.

- 14,816
- 3
- 48
- 60
If you are thinking about the client side of things, I would suggest checking out Matt Sukowski's 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 very much a live open source project, Matt Sukowsky handed it over to new caretakers this summer because he didnt feel he could spare enough time, and there has been lots and lots of commits since then. So Pest is better than ever for doing Rest in PHP :)

- 1
- 1

- 1,673
- 1
- 23
- 32