简体   繁体   English

如何创建RESTfull服务以从客户端检索json或xml文件

[英]How can I create a RESTfull service to retrieve a json or xml file from a client

I have to write a RESTfull service in PHP which can send json data to the caller and retrieve json (or xml) data from the user. 我必须用PHP编写一个RESTfull服务,该服务可以将json数据发送给调用方并从用户检索json(或xml)数据。 I know how to send json or xml data, but not how I can get data back from the user. 我知道如何发送json或xml数据,但不知道如何从用户那里取回数据。

A RESTful api usually doesnt expect a "response" from the client. RESTful api通常不希望客户端发出“响应”。 What you want to do is to create an endpoint for the client to POST the specific content. 您要做的是为客户端创建一个终端,以发布特定内容。

/api/user/?json={...}

On the PHP side you can retrieve the data with $_REQUEST['json'] (which includes both POST and GET. 在PHP方面,您可以使用$_REQUEST['json'] (包括POST和GET)检索数据。

There is something called HATEOS that can be used for telling the client about links associated with the current resource (if you want to "chain" calls between the client and the service). 有一种称为HATEOS的东西,可用于告知客户端与当前资源关联的链接(如果要在客户端和服务之间“链接”调用)。

The simplest way is getting JSON data as POST or PUT body. 最简单的方法是将JSON数据作为POST或PUT正文获取。 To get PHP body: 获得PHP主体:

$entityBody = file_get_contents('php://input');

and then decode json into a PHP object: 然后将json解码为PHP对象:

$requestBody = json_decode ( $entityBody);

If you are not sour the request body is XML or JSON then check Content-Type in the header. 如果您不满意,则请求正文为XML或JSON,然后检查标题中的Content-Type。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM