If I am issuing an HTTP DELETE, how can I access the PHP body/variable? I know if you're issuing POST, you access it via $_POST['varname'], but how do you access it if it's a DELETE not POST? Say the varname here is ID.

Comments

It is a duplicate of stackoverflow.com/questions/1405505/… but I don't know whether the answers help you.

Written by Felix Kling

Checked that out, doesn't help me

Written by aherlambang

You can get variable in $_GET. If the method is not GET and POST request will be sent as GET method

Written by Shakti Singh

I was half right with the parse_str idea, see my answer for an easy way parse the the PUT and DELETE request variables from the HTTP request body. There are limitations however.

Written by xzyfer

Accepted Answer

You might have to look at reading the data directly from the php://input stream.

I knwo this is how you have to do it for PUT requests and some quick googling indicates the method works for DELETE as well.

See here and here for examples. The comment here says this method works for DELETE also.

Here is an interesting code sample that will help (from sfWebRequest.class.php of the symfony 1.4.9 framework altered slight for brevity):

public function initialize(...) 
{
  ... code ...

  $request_vars = array();
  if (isset($_SERVER['REQUEST_METHOD']))
  {
    switch ($_SERVER['REQUEST_METHOD'])
    {
      case 'PUT':
        if ('application/x-www-form-urlencoded' === $this->getContentType())
        {
          parse_str($this->getContent(), $request_vars );
        }
        break;

      case 'DELETE':
        if ('application/x-www-form-urlencoded' === $this->getContentType())
        {
          parse_str($this->getContent(), $request_vars );
        }
        break;
    }
  ... more code ...
}

public function getContent()
{
  if (null === $this->content)
  {
    if (0 === strlen(trim($this->content = file_get_contents('php://input'))))
    {
      $this->content = false;
    }
  }

  return $this->content;
}

This code sample puts the PUT or DELETE request parameters into the $request_vars array. A limitation appears to be that the form (if you are using one other the content-type header) must be 'application/x-www-form-urlencoded', some quick googling confirms this.

Written by xzyfer
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki