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,
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/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?