PHP REST API 如何接收 PUTted 数据?

发布于 2024-09-25 18:28:09 字数 1749 浏览 2 评论 0 原文

我正在编写一个 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 方法,GETPOST,其中 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜柠檬 2024-10-02 18:28:09

使用服务器 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文