如何在服务器端访问 PHP REST API PUT 数据? [英] How do I access PHP REST API PUT data on the server side?

查看:25
本文介绍了如何在服务器端访问 PHP REST API PUT 数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-- 问题 --

我刚刚开始使用 REST API,并且很困惑.

I am just starting out with the REST API and am getting pretty confused.

这就是我的 PHP cRUL 客户端对于 PUT 的样子.

This is what my PHP cRUL client-side looks like for a PUT.

case 'PUT':
    curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT');
    curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
    break;

现在,当我查看服务器时,我的 $_SERVER['REQUEST_METHOD'] 显示 PUT,但我的问题是如何获取使用 CURLOPT_POSTFIELDS 发送的 $data.

Now when I look at the server my $_SERVER['REQUEST_METHOD'] shows PUT, but my question is how do I get the $data I sent with CURLOPT_POSTFIELDS.

我需要做的就是将与 PUT 请求一起发送的 $data 放入下一行.喜欢

All I need to do is get the $data sent with a PUT request into the next line. Like

$value = $data['curl_data'];

我在这个话题上看到了太多的混乱,这让我很头疼.在 php 客户端似乎很容易,但没有人有适用于 php 服务器端的答案.

I have seen so much clutter on this topic that it is giving me a headache. It seems so easy on the php client side, but no one has answers that are working for the php server side.

感谢您的帮助!

-- 回答(在帮助和作业之后)--

我是新人,所以直到 8 小时后我才能回答我自己的问题......奇怪:)

好的,在与这里的伟人一起工作之后,我不得不说我们找到了答案.我很自责,因为它太简单了,同时又让人困惑.

Okay, after working with the great people here I have to say we ran into the answer. I am kicking myself for it being so easy, at the same time it was confusing.

curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($handle, CURLOPT_POSTFIELDS, http_build_query($data));

第一个更改(上面)我必须在 $data 周围添加 http_build_query().这将我的数据从数组转换为 url 友好字符串.

The first change (above) I had to add http_build_query() around $data. This took my data from an array to a url friendly string.

接下来我必须添加.

parse_str(file_get_contents('php://input'), $put);

现在我可以做 $put['data'] 之类的事情了.

Now I can do things like $put['data'].

PaulPRO 上面给出的答案确实可以像 file_get_contents() 用更少的行那样获取数据.我们在试图弄清楚如何解析数据时陷入了困境,而这正是我在另一个网站上看到的 http_build_query() 缺失的原因.

The answer PaulPRO gave above does work to get the data the same way file_get_contents() did with less lines. We got stuck trying to figure out how to parse the data which was where my lack of http_build_query() I had seen on another site kicked into play.

这就是它的工作原理.

  1. 数据被放入普通数组中.
  2. http_build_query() 将其转换为一个很好的几乎类似于 GET 的字符串.
  3. file_get_contents() 将其从客户端传输到服务器.
  4. parse_str() 然后将其转回数组.

我看到很多关于使用 PUT 发送文件的消息.我可以看到这是如何工作的,但是从我在整个 REST 过程中读到的内容是,PUT 是更新数据,就像 post 是创建数据一样.也许我错了.我错过了什么吗?

I am seeing a lot of messages about using PUT to send files. I can see how this would work, but from what I read in this entire REST process was that PUT is to update data as post is to create data. Maybe I am mistaken. Am I missing something?

推荐答案

来自 PHP 手册:

PUT 数据来自标准输入:

PUT data comes from stdin:

$putdatafp = fopen("php://input", "r");

示例用法:

$putfp = fopen('php://input', 'r');
$putdata = '';
while($data = fread($putfp, 1024))
    $putdata .= $data;
fclose($putfp);

这篇关于如何在服务器端访问 PHP REST API PUT 数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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