HTTP protocol's PUT and DELETE and their usage in PHP

Introduction

I've read the following:

Hypertext Transfer Protocol (HTTP) is the life of the web. It's used every time you transfer a document, or make an AJAX request. But HTTP is surprisingly a relative unknown among some web developers.

The HTTP verbs comprise a major portion of our “uniform interface” constraint and provide us the action counterpart to the noun-based resource. The primary or most-commonly-used HTTP verbs (or methods, as they are properly called) are POST, GET, PUT , and DELETE .

Huh?

Well, we came to the point I lost track of things.

PUT and DELETE , they say. I've only ever heard of POST and GET and never saw something like $_PUT or $_DELETE passing by in any PHP code I've ever viewed.

My question

What are these methods (PUT) and (DELETE) for and if it's possible to use them in PHP, how would I go about this.

Note: I know this is not really a problem but I always grab a learning opportunity if I see one and would very much like to learn to use these methods in PHP if this is possible.


What are these methods (PUT) and (DELETE) for...

There are a lot of words to spend to explain this, and I'm not skilled enough to do it, but as already posted, a quick recap of what the HTTP specification describes.

The protocol basically says this:

  • use GET when you need to access a resource and retrieve data , and you don't have to modify or alter the state of this data.

  • use POST when you need to send some data to the server. Ex. from a form to save these data somewhere.

  • use HEAD when you need to access a resource and retrieve just the Headers from the response , without any resource data.

  • use PUT when you need to replace the state of some data already existing on that system.

  • use DELETE when you need to delete a resource (relative to the URI you've sent) on that system.

  • use OPTIONS when you need to get the communication options from a resource, so for checking allowed methods for that resource . Ex. we use it for CORS request and permissions rules.

  • You can read about the remaining two methods on that document, sorry I've never used it.
  • Basically a protocol is a set of rules you should use from your application to adhere to it.


    ... and if it's possible to use them in PHP, how would I go about this.

    From your application you should retrieve which method was used with $_SERVER['REQUEST_METHOD'] and react consequently.

    Some applications dealing with browsers that doesn't support PUT or DELETE methods use this trick, a hidden field from the html, with the value of ex.:

    <input name="_method" type="hidden" value="delete" />
    

    So from the application you're now able to recognize this as a DELETE request.


    Follow a simple description of how PHP handles the parameters:

    When you (your browser, your client) request a resource to an HTTP server you must use one of the method that the protocol (HTTP) accept. So you need to pass:

  • A METHOD
  • An Uri of the resource
  • Request Headers, like User-Agent, Host, Content-Length, etc
  • (Optional body of the request)
  • so a request should look like:

    GET /something/index.php?maybe=aparameter
    

    Now, inside you application, PHP take the parameters from this GET request and insert them in a superglobal (accessible allover your application) array. So you can access $_GET['maybe'] that returns 'aparameter`.

    In case of a POST request you ask for the resource to the HTTP server in a similar way but with an important difference. You provide the data inside the body of the request:

    POST /something/index.php
    
    (body) maybe=aparameter
    

    Now, inside you application, PHP from this POST request take the parameters and insert them in a superglobal (accessible allover your application) array. So you can access $_POST['maybe'] that returns 'aparameter`.

    Please pay very attention to Response Status Code too (ex. if you received a PUT request and you've updated that resource without error you should return a 204 status -No content-) .


    从PHP使用PUT数据的方式:

    $method = $_SERVER['REQUEST_METHOD'];
    if ('PUT' === $method) {
        parse_str(file_get_contents('php://input'), $_PUT);
        var_dump($_PUT); //$_PUT contains put fields 
    }
    

    What are these methods (PUT) and (DELETE)

    There are described in the HTTP spec.

    In a nutshell, and simplifying somewhat, PUT is for uploading a file to a URL and DELETE is for deleting a file from a URL.

    never sawy something like $_PUT or $_DELETE passing by in any PHP code I've ever viewed

    $_POST and $_GET are terribly named superglobals. $_POST is for data parsed from the request body. $_GET is for data parsed from the URL. There's nothing that strictly ties data in either of those places (especially the URL) to a particular request method.

    DELETE requests only care about the URL's path, so there is no data to parse.

    PUT requests usually care about the entire request body (not a parsed version of it) which you would access with file_get_contents('php://input'); .

    for and if it's possible to use them in PHP, how would I go about this.

    You'd need to map the URL onto a PHP script (eg with URL rewriting), test the request method, work out what URL you were actually dealing with, and then write code to do the appropriate action.

    链接地址: http://www.djcxy.com/p/41120.html

    上一篇: 航班请求在PUT / DELETE上返回406错误代码

    下一篇: HTTP协议的PUT和DELETE及其在PHP中的使用