get a PHP delete variable

前端 未结 4 1722
一整个雨季
一整个雨季 2021-02-07 04:25

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

4条回答
  •  遇见更好的自我
    2021-02-07 04:45

    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 for an example. 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.

提交回复
热议问题