简体   繁体   English

PHP 中的 REST API 集成

[英]REST API integration in PHP

My client has got some RESTful services written in ASP.net and front end code in PHP .我的客户有一些用ASP.net编写的RESTful服务和用PHP编写的前端代码。 He is providing me DTO classes and API url.他正在为我提供DTO类和API url。 Can anyone help me how to call these API services from PHP ?谁能帮助我如何从PHP调用这些API服务? I do not know how to make use of DTO classes in PHP我不知道如何在PHP使用DTO

The DTO classes will just specify the contract. DTO 类将只指定合同。 I would imagine they will map to JSON objects returned by the service (although you will want to check the serialization format).我想它们会映射到服务返回的 JSON 对象(尽管您需要检查序列化格式)。

You should look into deserializing JSON to PHP objects: http://php.net/manual/en/function.json-decode.php您应该研究将 JSON 反序列化为 PHP 对象: http : //php.net/manual/en/function.json-decode.php

Something like就像是

$url="http://www.website.com/api/object/1";
$json = file_get_contents($url);
$data = json_decode($json);

It doesnt seem trivial to decode to a specific class but you could declare a class for each DTO in PHP and instantiate the appropriate class from the client code and populate it with the data on the dynamic object that json_decode() returns.解码为特定类似乎并不容易,但您可以在 PHP 中为每个 DTO 声明一个类,并从客户端代码实例化适当的类,并使用 json_decode() 返回的动态对象上的数据填充它。

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

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