HTTP协议的PUT和DELETE以及它们在PHP中的用法 [英] HTTP protocol's PUT and DELETE and their usage in PHP

查看:148
本文介绍了HTTP协议的PUT和DELETE以及它们在PHP中的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简介



我已阅读以下内容:


超文本转移协议(HTTP)是Web的生命。每次传输文档或发出AJAX请求时都会使用它。但是,令人惊讶的是,HTTP在一些Web开发人员中相对不为人知。



HTTP谓词构成了我们统一接口约束的主要部分,并为我们提供了名词的动作对应基于资源。主要或最常用的HTTP谓词(或方法,因为它们被正确调用)是POST,GET, PUT DELETE




嗯?



嗯,我们已经忘记了事情。

PUT DELETE 。我只听说过 POST GET ,从未见过类似 $ _ PUT $ _ DELETE 在我查看的任何PHP代码中传递。



我的问题



这些方法(PUT)和(DELETE)是什么,如果可以在PHP中使用它们,我将如何解决这个问题。



注意:我知道这不是一个真正的问题但我总是抓住一个学习机会,如果我看到一个并且非常想学习使用这些方法PHP是否可行。

解决方案


这些方法(PUT)和(DELETE)是什么? ...


有很多词要用来解释这个,而且我不够熟练,但是如上所述,快速回顾一下 HTTP规范所描述的内容。



协议基本上这样说:




  • 当你需要访问资源并检索时,使用 GET 数据,您无需修改​​或更改此数据的状态。


  • 使用 POST 时您需要向服务器发送一些数据。防爆。从某个表单中保存这些数据。


  • 当您需要访问资源并仅检索资源时,使用 HEAD 来自回复的标题,没有任何资源数据。


  • 当您需要替换时,请使用 PUT


  • 当您需要删除时,请使用删除 <状态资源(相对于您发送的URI)。


  • 在需要时使用 OPTIONS 从资源中获取通信选项,以便检查该资源的允许方法。防爆。我们将它用于CORS请求和权限规则。


  • 您可以阅读该文档中剩余的两种方法,抱歉我从未使用过它。



协议基本上是一套规则,您应该在应用程序中使用这些规则来遵守它。







...如果可以
在PHP中使用它们,我该怎么做呢。


您应该从应用程序中检索与 $ _ SERVER ['REQUEST_METHOD'一起使用的方法] 并因此做出反应。



一些处理不支持PUT或DELETE方法的浏览器的应用程序使用这个技巧,来自html的隐藏字段,值为ex。:

 < input name =_ methodtype =hidden value =delete/> 

因此,从应用程序中,您现在可以将其识别为DELETE请求。






按照PHP处理参数的简单描述:



当您(您的浏览器,您的客户端)向HTTP服务器请求资源时,您必须使用协议(HTTP)接受的方法之一。所以你需要传递:




  • 方法

  • 资源的利用

  • 请求标题,如用户代理,主机,内容长度等

  • (请求的可选主体)



因此请求应如下所示:

  GET /something/index.php?maybe=aparameter 

现在,在你的应用程序中,PHP取来自此GET请求的参数,并将它们插入到超全局(可访问的应用程序)数组中。所以你可以访问 $ _ GET ['maybe'] 返回'aparameter`。



如果是POST请求您以类似的方式向HTTP服务器请求资源但具有重要区别。您在请求正文中提供数据:

  POST /something/index.php 

(正文)maybe = aparameter

现在,在你的应用程序中,来自这个POST请求的PHP接受参数和将它们插入超全局(可访问的应用程序)数组中。所以你可以访问 $ _ POST ['maybe'] 返回'aparameter`。



请支付非常注意响应状态代码(例如,如果您收到了PUT请求并且您已经更新了该资源而没有错误,则应该返回204状态 - 无内容 - )


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-).

这篇关于HTTP协议的PUT和DELETE以及它们在PHP中的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆