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

查看:81
本文介绍了如何在服务器端访问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.

这就是它的全部工作方式.

So This is how it all works.

  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天全站免登陆