I did google to understand what the purpose of PUT method is. I just found some texts. Can someone give me a php sample code with curl that I can understand better what the purpose of this method is and when and what should it be used for?
A put method is really no different than the interface for a get or post. The difference is only contextual/semantic in nature. For example, a request that would end up saving data to the database for an entity within your system would in theory be better as a put instead of a post. The standard Create and Edit CRUD operations are examples of requests better defined as put rather than post because the essence of those operations is to put data somewhere.
Please give some php code sample. Looking at curl on php site i see if using PUT it requires file info to output data in file. So how can i use it to output data as a new row in db?
To call a URL that manages a PUT request with Curl, it should look something like this:
// page_that_sends_a_put_request.php
$data = array("param1" => "value1");
$ch = curl_init('http://site.com/page_that_inserts_something_in_a_db.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$response = curl_exec($ch);
if(!$response) {
return false;
}
To use that request from a PHP file, it would look like:
// file: page_that_inserts_something_in_a_db.php
if($_SERVER['REQUEST_METHOD'] == 'GET') {
echo "this is a get request\
";
echo $_GET['param1']." is param1\
";
} elseif($_SERVER['REQUEST_METHOD'] == 'PUT') {
echo "this is a put request\
";
parse_str(file_get_contents("php://input"),$post_vars);
echo $post_vars['param1']." is param1\
";
// now you just need to insert the value of $param1 into a DB like usual.
}
Those PUT requests are used in web APIs / web services like REST, for example. For normal websites with links and forms, you don’t need to use that. Even for web services, you could use the GET method and still add rows in a database.
Sources:
http://www.lornajane.net/posts/2008/accessing-incoming-put-data-from-php
http://www.lornajane.net/posts/2009/putting-data-fields-with-php-curl
When using the GET method the presumption is that no data will be changed and so multiple get calls with the same data are allowed to use whatever was cached on return from the first call as the results for the subsequent calls without even passing the request to the server.
To not have the results cached from the first call to use for all subsequent calls you need to specify PUT or POST.
PUT implies a single set of data and that the information will be changed.
POST allows multiple sets of data and that the information may be changed.
Just because a particular action works doesn’t mean that it is the right one to use - you should not use GET if you are changing anything…
Thanks to all. Now my question is what is advantage of put lver post? I think the answer is above as: put use single set of data and post uses mutiple set of data? Is this the actuall difference of put over post? What do you exactly mean by single or multiple set of data?
A multiple set of data could be a multipart form - say a collection of text inputs and a file input. The text inputs are one set of data and the file is a second set of data. So you can’t send both in a single PUT but you can send both in a single POST.
Thanks. a
As about GET, when I pass a query like ?foo=bar to rest api, it is GET, but when I just read http://www.example.com/latestversion.php via curl, is it still a GET method? or is it not GET but simple reading content remotely?
PS. In order to accpet PUT method should I add this in .htaccess?
<Limit PUT>
order deny,allow
allow from all
</Limit>
All calls to the server will be one of HEAD, GET, PUT, POST - the normal requests for web pages use GET even where there are no parameters to pass.
Thanks, and what are the advantages of PUT over POST? (assuming we have single data set.)
Why not using POST always even if we have single data set?