承接 mdjward/php-rpc-client 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mdjward/php-rpc-client

最新稳定版本:1.0.0

Composer 安装命令:

composer require mdjward/php-rpc-client

包简介

Web Service RPC Client for PHP

README 文档

README

Web Service RPC client implementation for PHP.

This is a project I worked on a while back, so there are probably much better implementations out there at present.

The library aims to provide a simple, transparent means of working with RPC-based web services, using Guzzle as the underlying HTTP client library. At present, it supports:

  1. JSON-RPC (versions 1.0 and 2.0);
  2. XML-RPC;
  3. The RPC format used by the Transmission BitTorrent engine.

Making a request

Generally this library takes advantage of magic methods in PHP (specifically __get and __call), in much the same way as the PHP SOAP client to invoke web service methods under multiple levels of namespace.

Example: Updating XBMC's media library

The popular XBMC media centre makes use of JSON-RPC v2 in the extensive web service it offers for remote control purposes.

To invoke it's VideoLibrary.Scan method with an empty string parameter, on a server running on :

<?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("");

This should produce a string "OK", as the principle result of the method.

However, since magic method resolution is well known to have extremely poor performance, you are advised to extend the RequestInitiator class for purpose-specific APIs and create purpose-specific methods (and, potentially, fields/properties), as in the example below (although probably not with a public property, and possibly with dependency injection as opposed to tight coupling).

<?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("");

In this way, it is possible to build an entire client library for the XBMC RPC API or any supported RPC API.

统计信息

  • 总下载量: 15
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2015-02-10