我正在编写一个 API,并希望遵循 REST 方法。据我了解,如果我想让 API 用户更新特定记录 PUT http://server/specic应支持 _resource
类型请求。当然,他们不会执行 GET
并且需要传递新数据,我的问题是如何做到这一点(在我的具体情况下,仅更新指定的某些字段)记录,但这与这里不太相关)。我能想到两种方法: 在请求正文中包含数据(使用curl:curl -X PUT -d“key=value”http://server/specific_resource
)或在查询字符串中(使用curl:curl -X PUT http://server/specific_resource?key=value
)。
不幸的是,无论我采取什么方法,似乎都很难获得所提供的数据。问题似乎在于 PHP 仅真正完全理解两种 HTTP 方法,GET
和 POST
,其中 PUT
被视为用于文件上传。如果我将数据包含在正文中,那么访问它的唯一方法似乎是 通过 fopen('php://input')
调用。例如, http_get_request_body()
不提供数据。同样,在 $_REQUEST
超全局中也找不到该信息。如果我不想使用 fopen('php://input')
处理原始请求正文,那么看来我必须将数据作为查询字符串参数发送,因为数据将显示为 $_GET
超全局元素($_REQUEST
也是如此)。
我专门使用 CakePHP,它似乎只填充我的控制器方法 params 数组的 form
数组。 com/group/cake-php/browse_thread/thread/e50ee7b72b77ee53" rel="nofollow">如果请求是 POST。如果在请求 URL 中使用,则无论请求方法如何,查询字符串参数都会放入 params
' url
数组中。毫不奇怪,我不是唯一一个跑步的人进入这个。
您建议的解决方案是什么?处理输入流?使用查询字符串参数?只是忘记 PUT
动词并使用 POST
代替吗?
I'm writing an API and want to follow the REST approach. As I understand it, if I want to let API users update specific records a PUT http://server/specific_resource
type request should be supported. Of course, they're not doing a GET
and they'll need to pass along the new data and my question is how to do this (in my specific case only updating some of the fields of the specified record, but that's not so relevant here). There are two approaches I can think of: including the data in the request body (using curl: curl -X PUT -d "key=value" http://server/specific_resource
) or in a query string (using curl: curl -X PUT http://server/specific_resource?key=value
).
Unfortunately, regardless of the approach I take, it seems very hard to get the provided data. The problem seems to be that PHP only really completely understands two HTTP methods, GET
and POST
, with PUT
considered to be for file uploads. If I include the data in the body then the only way to access it seems to be via an fopen('php://input')
call. For instance, http_get_request_body()
doesn't provide the data. Likewise, the information can't be found in the $_REQUEST
superglobal. If I don't want to have to process the raw request body with fopen('php://input')
then it appears that I must send the data as query string parameters, as the data will appear as elements of the $_GET
superglobal (and so also $_REQUEST
).
I'm specifically using CakePHP and it seems to only populate the form
array of the params
array in my controller method if the request was a POST. Query string parameters are put in params
' url
array regardless of the request method if used in the request URL. Not surprisingly, I'm not the only one who has run into this.
What is the solution that you'd suggest? Process the input stream? Use query string parameters? Just forget the PUT
verb and use POST
instead?
发布评论
评论(2)
尝试这篇关于解析 REST 接口的 PUT 数据的博客文章PHP。
Try this blog entry about parsing PUT data for a REST interface in PHP.
使用服务器 http 方法变量检查其是否为 PUT。
我建议你看一下 SLIM 源代码,看看他们是如何处理它的
!
Use the server http method variable to check if its a PUT.
I sugget you to take a look at SLIM source code to check how they handle it
cheer!