定制 jalle19/simple-json-rpc-client 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

jalle19/simple-json-rpc-client

Composer 安装命令:

composer require jalle19/simple-json-rpc-client

包简介

Simple JSON-RPC 2.0 client which utilizes Zend for HTTP functionality

关键字:

README 文档

README

Build Status Scrutinizer Code Quality Coverage Status

simple-json-rpc-client

Simple yet powerful JSON-RPC client which fully implements the JSON-RPC 2.0 specifications. It provides an interface for creating custom clients and comes with a default implementation which sends requests over HTTP using POST. Being standard-compliant it supports standard requests, notifications as well as batch requests.

Requirements

PHP 5.4 is required.

Installation

Install using Composer (the package is published on Packagist). Install with --no-dev if you don't want or need the ability to run the test suite.

Usage

Standard requests

<?php

use SimpleJsonRpcClient\Client\HttpPostClient as Client;

use SimpleJsonRpcClient\Request\Request;
use SimpleJsonRpcClient\Exception\BaseException;
use SimpleJsonRpcClient\Response\Response;

// Initialize the client. Credentials are optional.
$client = new Client('localhost', 'username', 'password');

try 
{
	// Send a request without parameters. The "id" will be added automatically unless supplied.
	// Request objects return their JSON representation when treated as strings.
	$request = new Request('method');
	$response = $client->sendRequest($request);
	
	// Send a request with parameters specified as an array
	$request = new Request('method', array('param1'=>'value1'));
	$response = $client->sendRequest($request);
	
	// Send a request with parameters specified as an object
	$params = new stdClass();
	$params->param1 = 'value1';
	$request = new Request('method', $params);
	$response = $client->sendRequest($request);
	
	// Send a parameter-less request with specific "id"
	$request = new Request('method', null, 2);
	$response = $client->sendRequest($request);
}
catch (BaseException $e) 
{
	echo $e->getMessage();
}

Notifications

<?php

use SimpleJsonRpcClient\Client\HttpPostClient as Client;

use SimpleJsonRpcClient\Request\Notification;
use SimpleJsonRpcClient\Exception\BaseException;

$client = new Client('localhost', 'username', 'password');

try 
{
	$request = new Notification('notification');
	$client->sendNotification($request);
}
catch (BaseException $e) 
{
	echo $e->getMessage();
}

Batch requests

<?php

use SimpleJsonRpcClient\Client\HttpPostClient as Client;

use SimpleJsonRpcClient\Request;
use SimpleJsonRpcClient\Exception\BaseException;

$client = new Client('localhost', 'username', 'password');

try 
{
	$request = new Request\BatchRequest(array(
		new Request\Request('method'),
		new Request\Notification('anotherMethod'),
		new Request\Request('yetAnotherMethod', null, 3)
	));
	
	$batchResponse = $client->sendBatchRequest($request);
	
	// Retrieve all response objects
	$responses = $batchResponse->getResponses();
	
	// Get specific response
	$response = $batchResponse->getResponse(3);
}
catch (BaseException $e) 
{
	echo $e->getMessage();
}

Exception handling

All exceptions derive from the base class BaseException. If you don't want to handle specific exceptions differently from others you can simply catch BaseException like in the examples above. Here's an example which illustrates the exception hierarchy:

<?php

use SimpleJsonRpcClient\Client\HttpPostClient as Client;

use SimpleJsonRpcClient\Request;
use SimpleJsonRpcClient\Exception;
use SimpleJsonRpcClient\Response;

$client = new Client('localhost', 'username', 'password');

try 
{
	$request = new Request\Request('method', array('param1'=>'value1'));
	$response = $client->sendRequest($request);
}
catch (ClientException $ce) {
	// The client failed to execute the request
}
catch (InvalidResponseException $e) 
{
	// The response was invalid, e.g. it could not be parsed or it is not standard-compliant
}
catch (ResponseErrorException $re) {
	// The request itself was successful but the JSON-RPC response indicates an error.
	// A subclass of ResponseErrorException is thrown for pre-defined errors (see http://www.jsonrpc.org/specification#error_object)
}
catch (Exception $e) {
	// Anything else, usually InvalidArgumentException
}

Error handling

Some JSON-RPC servers may return a "data" property in their response error. This property may contain valuable information as to the nature of the error. The special ResponseErrorException is thrown whenever a response indicates an error. The exception has a getData() method which returns an object representation of the error data.

try {
	$response = $client->sendRequest($request);
}
catch(ResponseErrorException $e) {
	$data = $e->getData();
}

Flags

The client constructor takes a set of flags as the forth parameter. These flags can be used to alter the behavior of the client, mostly useful for working with buggy servers. For example, the FLAG_ATTEMPT_UTF8_RECOVERY flag will cause the Response class to attempt to avoid "Malformed UTF-8 in response" errors by re-encoding the raw response as UTF-8 before passing it to json_decode(). This is only done if the raw response is determined not to be valid UTF-8.

Test suite

Run vendor/bin/phpunit in the project root folder to run the unit tests. The test suite will launch a mock JSON-RPC server on localhost:8585 using PHP's internal web server. If this port is not available on your system you can change it by editing phpunit.xml.

License

This code is licensed under the New BSD License

统计信息

  • 总下载量: 4.18k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 3
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: BSD-2-Clause
  • 更新时间: 2013-08-06

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固