HTTP 协议的 PUT 和 DELETE 及其在 PHP 中的使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27941207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTTP protocol's PUT and DELETE and their usage in PHP
提问by
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.
超文本传输协议 (HTTP) 是网络的生命。每次传输文档或发出 AJAX 请求时都会使用它。但令人惊讶的是,HTTP 在一些 Web 开发人员中相对不为人知。
HTTP 动词构成了我们“统一接口”约束的主要部分,并为我们提供了与基于名词的资源对应的动作。主要或最常用的 HTTP 动词(或方法,因为它们被正确调用)是 POST、GET、PUT和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.
PUT
并且DELETE
,他们说。我只听说过POST
和GET
,从来没有见过类似的东西$_PUT
或$_DELETE
由我曾经看到任何PHP代码传递。
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.
这些方法 (PUT) 和 (DELETE) 的用途是什么,如果可以在 PHP 中使用它们,我将如何处理。
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.
注意:我知道这不是一个真正的问题,但如果我看到一个学习机会,我总是抓住一个学习机会,如果可能的话,我非常想学习在 PHP 中使用这些方法。
采纳答案by ilpaijin
What are these methods (PUT) and (DELETE) for...
这些方法 (PUT) 和 (DELETE) 是什么...
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 specificationdescribes.
有很多词要解释这一点,我不够熟练,无法做到这一点,但正如已经发布的那样,快速回顾一下HTTP 规范描述的内容。
The protocol basically says this:
该协议基本上是这样说的:
use GETwhen you need to access a resource and retrieve data, and you don't have to modify or alter the state of this data.
use POSTwhen you need to send some datato the server. Ex. from a form to save these data somewhere.
use HEADwhen you need to access a resource and retrieve just the Headers from the response, without any resource data.
use PUTwhen you need to replace the state of some data already existingon that system.
use DELETEwhen you need to delete a resource(relative to the URI you've sent) on that system.
use OPTIONSwhen 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.
当您需要访问资源和检索数据时使用GET,并且您不必修改或更改此数据的状态。
当您需要向服务器发送一些数据时使用POST。前任。从一个表单将这些数据保存在某处。
当您需要访问资源并仅从响应中检索 Headers而没有任何资源数据时,请使用HEAD。
当您需要替换该系统上已存在的某些数据的状态时,请使用PUT。
当您需要删除该系统上的资源(相对于您发送的 URI)时,请使用DELETE。
当您需要从资源中获取通信选项时使用OPTIONS,以便检查该资源的允许方法。前任。我们将其用于 CORS 请求和权限规则。
- 您可以阅读该文档中剩余的两种方法,抱歉我从未使用过它。
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.
...如果可以在 PHP 中使用它们,我将如何处理。
From your application you should retrieve which method was used with $_SERVER['REQUEST_METHOD']
and react consequently.
从您的应用程序中,您应该检索使用了哪种方法$_SERVER['REQUEST_METHOD']
并做出相应的反应。
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.:
一些处理不支持 PUT 或 DELETE 方法的浏览器的应用程序使用这个技巧,一个来自 html 的隐藏字段,其值为 ex.:
<input name="_method" type="hidden" value="delete" />
So from the application you're now able to recognize this as a DELETE request.
因此,从应用程序中,您现在可以将其识别为 DELETE 请求。
Follow a simple description of how PHP handles the parameters:
遵循PHP如何处理参数的简单描述:
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:
当您(您的浏览器、您的客户端)向 HTTP 服务器请求资源时,您必须使用协议 (HTTP) 接受的方法之一。所以你需要通过:
- A METHOD
- An Uri of the resource
- Request Headers, like User-Agent, Host, Content-Length, etc
- (Optional body of the request)
- 一个方法
- 资源的 Uri
- 请求标头,如 User-Agent、Host、Content-Length 等
- (请求的可选正文)
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`.
现在,在您的应用程序中,PHP 从这个 GET 请求中获取参数并将它们插入到一个超全局(整个应用程序均可访问)数组中。因此,您可以访问$_GET['maybe']
返回“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 请求的情况下,您以类似的方式向 HTTP 服务器请求资源,但有一个重要的区别。您在请求正文中提供数据:
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`.
现在,在您的应用程序中,来自此 POST 请求的 PHP 获取参数并将它们插入到一个超全局(可在整个应用程序中访问)数组中。因此,您可以访问$_POST['maybe']
返回“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-).
也请非常注意响应状态代码(例如,如果您收到 PUT 请求并且您已正确更新该资源,则应返回 204 status -No content-)。
回答by AndreyP
Way to use PUT data from PHP:
从 PHP 中使用 PUT 数据的方法:
$method = $_SERVER['REQUEST_METHOD'];
if ('PUT' === $method) {
parse_str(file_get_contents('php://input'), $_PUT);
var_dump($_PUT); //$_PUT contains put fields
}
回答by CodeCaster
PHP's $_GET
and $_POST
are poorly named. $_GET
is used to access the values of query string parameters, and $_POST
lets you access the request body.
PHP 的$_GET
和$_POST
名称很差。$_GET
用于访问查询字符串参数的值,并$_POST
允许您访问请求正文。
Using query string parameters is not limited to GET requests, and other kinds of requests than just POST can come with a request body.
使用查询字符串参数不仅限于 GET 请求,除了 POST 之外的其他类型的请求都可以带有请求正文。
If you want to find out the verb used to request the page, use $_SERVER['REQUEST_METHOD']
.
如果您想找出用于请求页面的动词,请使用$_SERVER['REQUEST_METHOD']
.
回答by Ashish Awasthi
Most suitable place to use these (PUT and DELETE) methods is REST API. Where we use http methods to define the mode of operation for example you want to fetch any resources then you can use following:
最适合使用这些(PUT 和 DELETE)方法的地方是 REST API。在我们使用 http 方法来定义操作模式的地方,例如您想要获取任何资源,那么您可以使用以下内容:
GET http://api.example.com/employee/<any_id>
to add a new item:
添加新项目:
POST http://api.example.com/employee/
to Update or Edit:
更新或编辑:
PUT http://api.example.com/employee/
to Delete an existing resource:
删除现有资源:
DELETE http://api.example.com/employee/1
etc.
等等。
Now on PHP
side you just need to read what HTTP
method used so that you can make an action according to that.
现在,PHP
您只需要阅读HTTP
使用的方法,以便您可以根据该方法进行操作。
There are lots of libraries available which can do that for you.
有很多可用的库可以为您做到这一点。
回答by Quentin
What are these methods (PUT) and (DELETE)
这些方法是什么(PUT)和(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.
简而言之,稍微简化一下,PUT
用于将文件上传到 URL 和DELETE
用于从 URL 删除文件。
never sawy something like
$_PUT
or$_DELETE
passing by in any PHP code I've ever viewed
从来没有像我看过的任何 PHP 代码那样
$_PUT
或在其中$_DELETE
传递
$_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.
$_POST
并且$_GET
被可怕地命名为超全球。$_POST
用于从请求正文解析的数据。$_GET
用于从 URL 解析的数据。没有任何东西可以将这两个位置(尤其是 URL)中的数据与特定请求方法严格联系起来。
DELETE requests only care about the URL's path, so there is no data to parse.
DELETE 请求只关心 URL 的路径,因此没有要解析的数据。
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');
.
PUT 请求通常关心您将使用file_get_contents('php://input');
.
for and if it's possible to use them in PHP, how would I go about this.
如果可以在 PHP 中使用它们,我将如何处理。
You'd need to map the URL onto a PHP script (e.g. 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.
您需要将 URL 映射到 PHP 脚本(例如使用URL 重写),测试请求方法,找出您实际处理的 URL,然后编写代码来执行适当的操作。