17

I never see how is PUT/DELETE request sent.

How to do it in PHP?

I know how to send a GET/POST request with curl:

$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);

But how to do PUT/DELETE request?

2
  • To get an PUT request, you cant do $_PUT['var'], but: parse_str(file_get_contents('php://input'), $put_vars); Commented Jan 28, 2010 at 12:03
  • $_PUT will fail,but php://input works. Commented Jan 28, 2010 at 12:21

2 Answers 2

49

For DELETE use curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
For PUT use curl_setopt($ch, CURLOPT_PUT, true);

An alternative that doesn't rely on cURL being installed would be to use file_get_contents with a custom HTTP stream context.

$result = file_get_contents(
    'http://example.com/submit.php', 
    false, 
    stream_context_create(array(
        'http' => array(
            'method' => 'DELETE' 
        )
    ))
);

Check out these two articles on doing REST with PHP

Sign up to request clarification or add additional context in comments.

3 Comments

It's working and I voted for that, but the drawback is that 'remote url opening ' via file_get_contents may be blocked in configuration and that has be taken into account
@Ekonoval sure, just like it has to be taken into account that cURL might not be installed :)
FYI, links are no longer valid, ?s=restful returns no relevant results.
4

Generally speaking, if you want to send some "non-GET" request, you'll often work with curl.


And you'll use the curl_setopt function to configure the request you're sending ; amongst the large amount of possible options, to change the request method, you'll be interested by at least those options (quoting) :

  • CURLOPT_CUSTOMREQUEST : A custom request method to use instead of "GET" or "HEAD" when doing a HTTP request. This is useful for doing "DELETE" or other, more obscure HTTP requests.
  • CURLOPT_HTTPGET : TRUE to reset the HTTP request method to GET.
  • CURLOPT_POST : TRUE to do a regular HTTP POST.
  • CURLOPT_PUT : TRUE to HTTP PUT a file. The file to PUT must be set with CURLOPT_INFILE and CURLOPT_INFILESIZE.


Of course, curl_setopt is not the only function you'll use ; see the documentation page of curl_exec for an example of how to send a request with curl.

(Yes, that example is pretty simple, and sends a GET request -- but you should be able to build from there ;-) )

3 Comments

CURLOPT_POST is used to send GET/DELETE request,what about PUT/DELETE?
for PUT, you have CURLOPT_PUT, and for DELETE, it seems you'll have to use CURLOPT_CUSTOMREQUEST
Correct. That is what it says in the second article I've linked as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.