mdjward / php-rpc-client
此包的最新版本(1.0.0)没有提供许可信息。
PHP的Web服务RPC客户端
1.0.0
2015-02-10 15:26 UTC
Requires
- php: >=5.3
- guzzle/guzzle: 3.3.*
Requires (Dev)
- phpunit/phpunit: 4.1.1
This package is not auto-updated.
Last update: 2024-09-18 07:43:10 UTC
README
PHP的Web服务RPC客户端实现。
这是一个我之前工作的项目,因此目前可能存在许多更好的实现。
该库旨在通过使用Guzzle作为底层HTTP客户端库,提供一种简单、透明的与基于RPC的Web服务协同工作的方式。目前,它支持以下内容:
发送请求
一般来说,此库利用PHP中的魔术方法(特别是__get和__call),类似于PHP SOAP客户端,在多个命名空间级别下调用Web服务方法。
示例:更新XBMC的媒体库
流行的XBMC媒体中心在其为远程控制提供的广泛Web服务中使用了JSON-RPC v2。
要在运行在
<?php use Guzzle\Http\Client; use Mdjward\RpcApi\RequestInitiator; use Mdjward\RpcApi\RequestEncoder\JsonRpc\JsonRpcV2Encoder; // Initialise the Guzzle client to the JSON RPC endpoint offered by XBMC $client = new Client("http://xbmc.myhomenetwork.org:8080/jsonrpc"); // Initialise an object of the prescribed class for encoding/decoding JSON RPC v2 messages $encoder = new JsonRpcV2Encoder(); // Initialise the initiator! $initiator = new RequestInitiator($client, $encoder); // Invoke the Scan method and store the response in variable $response $response = $initiator->VideoLibrary->Scan("");
这将生成一个字符串“OK”,作为该方法的原理结果。
然而,由于魔术方法解析众所周知性能极差,建议您为特定API扩展RequestInitiator类,并创建特定方法(以及可能的字段/属性),如下例所示(尽管可能不是公共属性,可能使用依赖注入而不是紧密耦合)。
<?php // ... class XbmcRequestInitiator extends RequestInitiator { public $videoLibrary; public function __construct(Client $client, JsonRpcV2Encoder $encoder) { parent::__construct($client, $encoder, ""); $this->videoLibrary = new VideoLibraryRequestInitiator($client, $encoder); } } // ... class VideoLibraryRequestInitiator extends RequestInitiator { public function __construct(Client $client, JsonRpcV2Encoder $encoder) { parent::__construct($client, $encoder, "VideoLibrary"); } public function scan($directory = "") { return $this->__call("Scan", array("directory" => $directory); } } // ... // Initialise the XBMC initiator! $initiator = new XbmcRequestInitiator($client, $encoder); // Invoke the Scan method and store the response in variable $response $response = $initiator->videoLibrary->scan("");
通过这种方式,可以构建用于XBMC RPC API或任何支持的RPC API的完整客户端库。