3

I'm getting confused with the tutorials/ examples on RESTful PHP. Most of them are using frameworks (such as Slim) and ending up as APIs and that confuse me more. I would like to avoid all frameworks and API makings from start first before understanding thoroughly how to create a simple RESTful PHP.

I understand that,

REST, at it's core, is really just about using the right HTTP verb for the job. GET, POST, PUT, DELETE all have meanings, and if something is described as being RESTful, all it really means is that the site/app in question adheres to those meanings.

And from the php server side, I understand this is how I can detect the REST request types,

$_SERVER['REQUEST_METHOD']

But I main problem is how to send these REST types (GET, POST, PUT, DELETE) via URLs to the server?

For instance from this tutorial,

A PUT request is used when you wish to create or update the resource identified by the URL. For example,

http://restfulphp.com/clients/robin

Then,

DELETE should perform the contrary of PUT; it should be used when you want to delete the resource identified by the URL of the request.

http://restfulphp.com/clients/anne

http://restfulphp.com/clients/robin and http://restfulphp.com/clients/anne are the same clean URL pattern. How can I know that the former is meant for PUT and the latter is meant for DELETE? Or where should I set in the form/ html to differentiate them?

And it gets complicated when curl comes in - what is it to do with a REST website?

From that tutorial above,

Once you have cURL installed, type: 
curl -v google.com

Where should I type??

How curl is going to help me to DELETE or to PUT? How $_SERVER['REQUEST_METHOD'] is going to detect REST type from curl?

curl -v -X DELETE /clients/anne

and

curl -v -X PUT -d "some text"

The questions I asked may sound stupid but it would great if someone can help me to understand these.

EDIT:

Argg PHP cURL - another thing that confuses me further and deeper! Why do I need it if I am going to send REST request types via XMLHttpRequest object? It seems that it is meant for communicating with other API providers which I want to avoid at this stage.

And if I can send REST request types with PHP cURL within my local website, how and where should I place these lines from this Q&A,

$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
curl_setopt($ch, CURLOPT_COOKIEFILE,$cookieFile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch,   CURLOPT_SSL_VERIFYPEER,   FALSE);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);

If I use PHP cURL then what happens to $_SERVER['REQUEST_METHOD'] at the server side?

Community
  • 1
  • 1
Run
  • 54,938
  • 169
  • 450
  • 748
  • http://stackoverflow.com/questions/19257295/send-put-request-with-php-curl – s3v3n Jan 17 '14 at 10:02
  • This: `curl -v -X DELETE /clients/anne` was an example of how could you make a DELETE request from command-line. Take a look at the previous link to see how you can make different types of requests using `PHP`. – s3v3n Jan 17 '14 at 10:03
  • possible duplicate of [How to start a GET/POST/PUT/DELETE request and judge request type in PHP?](http://stackoverflow.com/questions/2153650/how-to-start-a-get-post-put-delete-request-and-judge-request-type-in-php) – s3v3n Jan 17 '14 at 10:03
  • `PHP cURL` that confuses me too! What is it for in REST? Where should I use/ put it to send REST types? – Run Jan 17 '14 at 10:05

4 Answers4

6

http://restfulphp.com/clients/robin and http://restfulphp.com/clients/anne are the same clean URL pattern. How can I know that the former is meant for PUT and the latter is meant for DELETE?

You can't tell from the URL. The client has to couple the URL with an HTTP verb. Part of the point of REST is that you might want to do different things to the same resource (there isn't much point if PUTting something if you will never want to GET it in the future).

Or where should I set in the form/ html to differentiate them?

An HTML form can only trigger POST and GET requests. If you want to use a different HTTP verb from a webpage then you need to use JavaScript and the XMLHttpRequest object.

And it gets complicated when curl comes in - what is it to do with a REST website?

curl is a library (and a command line client that uses that library) for making HTTP requests with.

When you are developing a REST interface, you can use the command line client to test your server. You can also use it as an actual client, but you would probably be better off writing a dedicated tool so you have have a nice command line interface instead of one that forces you to deal with the HTTP side of things whenever you use it.

As a library, you can use it to write clients for your REST interface.

Where should I type??

In a command line shell (such as bash or Windows Power Shell) in a terminal emulator (such as XTerm or Windows Power Shell).

How curl is going to help me to DELETE or to PUT?

It lets you specify the HTTP verb you send. You can see that in the examples you put after your question.

How $_SERVER['REQUEST_METHOD'] is going to detect REST type from curl?

It tells you which verb was used to make the request.

Why do I need PHP cURL if I am going to send REST request types via XMLHttpRequest object?

You don't. You can use PHP if you want to make requests from your server. You can use XMLHttpRequest if you want to make requests from your browser.

It seems that it is meant for communicating with other API providers which I want to avoid at this stage.

You can use it to communicate between a customer facing web server that you control and a backend system with a (non-customer facing) HTTP interface that you also control.

And if I can send REST request types with PHP cURL within my local website, how and where should I place these lines

At the point in your server side code (or your other PHP client) that you want to make the request to the REST service.

If I use PHP cURL then what happens to $_SERVER['REQUEST_METHOD'] at the server side?

Then, assuming that you are using server side PHP (and not, for instance, command line PHP):

  1. The browser will make a request to your server
  2. $_SERVER['REQUEST_METHOD'] will be the method the browser makes the request with
  3. The PHP running on the server will make a request to some other server (or possibly another part of the same server)
  4. $_SERVER['REQUEST_METHOD'] will be the method the PHP makes the request with
  5. The server for the second request will respond to the PHP client script
  6. The server for the first request will respond to the browser
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Thanks. Can I ask - is `curl` as a library` same as `PHP cURL`? How `PHP cURL` can send REST request types to the server with clean URLs above in my questions? – Run Jan 17 '14 at 10:31
  • Yes, the PHP cURL API is a wrapper about the common cURL library. Use setopt and CURLOPT_CUSTOMREQUEST – Quentin Jan 17 '14 at 10:38
  • thanks. so if I use `PHP cURL` then I can ditch `XMLHttpRequest` object? or in other words why do I need `PHP cURL` if I am going to send REST request types via `XMLHttpRequest` object? Please see my edit above. thanks! – Run Jan 17 '14 at 10:41
  • 1
    You need to use XMLHttpRequest if you want to send the requests from the browser. You need to use PHP cURL (or another PHP HTTP library) if you want to send the requests from PHP. – Quentin Jan 17 '14 at 10:43
  • So they (XMLHttpRequest & PHP cURL) do the same thing - just in different languages? – Run Jan 17 '14 at 10:46
  • 1
    The "from the browser" / "from the server" difference is generally more important then the "JavaScript" / "PHP" difference. (Edited the answer to address the edits to your question). – Quentin Jan 17 '14 at 10:49
  • THanks for the edit. Now it all makes sense! THanks so much for the help! Two thumbs up:-) – Run Jan 17 '14 at 10:51
2

First of all: you are right about the POST, GET, DELETE, PUT methods. cURL(http://nl1.php.net/curl) is important if you need to call another webservice from your code(you can execute POSTs, PUTs, DELETEs, GETs). If you just want to test your webservices you could use RESTcliet(https://addons.mozilla.org/nl/firefox/addon/restclient/). With this tool you can clone these methods easily and see the result.

Ofcourse you have to define in your HTML the form method to call the right webservce:

<form method="PUT_YOUR_METHOD" action="WS_URL">

It also possible to call these methods from jQuery, javascript, Java(Android), Objective C(iOS) and so on.

GuyT
  • 4,316
  • 2
  • 16
  • 30
  • Thanks. Can I use PUT, DELETE in the from's method attribute, such as `method="PUT"`, or `method="DELETE"`?? – Run Jan 17 '14 at 10:14
  • That's not supported by every major browser so far as I know(see http://stackoverflow.com/questions/165779/are-the-put-delete-head-etc-methods-available-in-most-web-browsers for more detailed information). Alternatively most use `XMLHttpRequest` what is accepted by all major browsers. – GuyT Jan 17 '14 at 10:17
  • `XMLHttpRequest` - then it has to do with ajax then? – Run Jan 17 '14 at 10:18
  • 1
    Yes, it has. Which method you want to use is up to yours(please first read http://stackoverflow.com/questions/2152352/xmlhttprequest-vs-jquery). – GuyT Jan 17 '14 at 10:20
  • That question is from 2010. There's not much point in using jQuery for XHR compatibility these days. It's bloat. – Quentin Jan 17 '14 at 10:36
1

But I main problem is how to send these REST types (GET, POST, PUT, DELETE) via URLs to the server?

How you send a the request type depends on what you're using. For example as you mentioned curl can use:

curl -v -X DELETE /clients/anne

To send a delete request. $_SERVER['REQUEST_METHOD'] on the server will then contain the appropriate request type and the script can decide what to do (in this case delete the resource provided the user has access).

In jQuery you can use:

$.ajax({
    url: '/clients/anne',
    type: 'DELETE',
    success: function(result) {
        // Success!
    }
});

Where should I type??

In that example the curl command would be typed into the command line. PHP also has a curl library that you could use but the syntax is different.

Or where should I set in the form/ html to differentiate them?

In theory you would place, say, "PUT" in the forms method attribute. However see this question.

Community
  • 1
  • 1
Jim
  • 22,354
  • 6
  • 52
  • 80
  • thanks. with jquery's ajax is easy to determine the REST types. what if jquery is not in the picture? – Run Jan 17 '14 at 10:11
  • @lauthiamkok How do you mean determine the REST types? – Jim Jan 17 '14 at 10:13
  • for instance you determine it by using jquery ajax - `type: 'DELETE'`. that is easy with jquery. what if with a plain form without jquery? – Run Jan 17 '14 at 10:16
  • 1
    @lauthiamkok Plain forms can only send `POST` or `GET` requests. You specify them with the form attribute: `
    `.
    – Jim Jan 17 '14 at 10:39
  • Can I ask - if I am going to use `XMLHttpRequest` in jquery, why someone suggesting `PHP cURL` in the comments? – Run Jan 17 '14 at 10:44
  • 1
    @lauthiamkok It depends what you are going to use it for. If it's for a webpage that will send off REST requests to your server then `XMLHttpRequest` will allow that page to do that. However if your server is running PHP and wants to call a REST API then you could use PHP cURL. – Jim Jan 17 '14 at 10:50
  • Thanks Jim. I got it now :) – Run Jan 17 '14 at 12:19
1

You're asking two questions.

  1. "(...) the same clean URL pattern. How can I know that the former is meant for PUT and the latter is meant for DELETE?"

    It isn't. The idea of REST:

    1. The information in your application is structured as a collection of resources.
    2. Each resource is addressable: it has a URL.
    3. All operations on the information are expressed in terms of elementary CRUD operations on resources.
    4. The HTTP verbs are used to implement the different operations.

     
    So you don't use URLs to express operations; you use them to express the objects being operated on.

  2. "Where should I type??''

    Into a command line interpreter such as CMD (on Windows) or bash (on Linux). You probably need to install curl first.

reinierpost
  • 8,425
  • 1
  • 38
  • 70