• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

nuxwin/JsonRPC: A simple Json-RPC PHP client/server that just works.

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

nuxwin/JsonRPC

开源软件地址:

https://github.com/nuxwin/JsonRPC

开源编程语言:


开源软件介绍:

JsonRPC - PHP Client and Server

A simple Json-RPC client/server that just works. There is only 2 files.

Features

  • JSON-RPC 2.0 protocol only
  • The server support batch requests and notifications
  • Authentication and IP based client restrictions
  • License: Unlicense http://unlicense.org/

Requirements

  • The only dependency is curl and Reflection classes
  • Works only with PHP >= 5.3

Examples

Server

<?php

require 'JsonRPC/Server.php';

use JsonRPC\Server;

$server = new Server;

// Procedures registration

$server->register('addition', function ($a, $b) {

    return $a + $b;
});

$server->register('random', function ($start, $end) {

    return mt_rand($start, $end);
});

// Return the response to the client
echo $server->execute();

Client

Example with positional parameters:

<?php

require 'JsonRPC/Client.php';

use JsonRPC\Client;

$client = new Client('http://localhost/server.php');
$result = $client->execute('addition', array(3, 5));

var_dump($result);

Example with named arguments:

<?php

require 'JsonRPC/Client.php';

use JsonRPC\Client;

$client = new Client('http://localhost/server.php');
$result = $client->execute('random', array('end' => 10, 'start' => 1));

var_dump($result);

Arguments are called in the right order. If there is an error, the execute() method return NULL.

IP based client restrictions

The server can allow only some IP adresses:

<?php

require 'JsonRPC/Server.php';

use JsonRPC\Server;

$server = new Server;

// IP client restrictions
$server->allowHosts(array('192.168.0.1', '127.0.0.1'));

// Procedures registration

[...]

// Return the response to the client
echo $server->execute();

If the client is blocked, you got a 403 Forbidden HTTP response.

HTTP Basic Authentication

If you use HTTPS, you can allow client by using a username/password.

<?php

require 'JsonRPC/Server.php';

use JsonRPC\Server;

$server = new Server;

// List of users to allow
$server->authentication(array('jsonrpc' => 'toto'));

// Procedures registration

[...]

// Return the response to the client
echo $server->execute();

On the client, set the credentials like that:

<?php

require 'JsonRPC/Client.php';

use JsonRPC\Client;

$client = new Client('http://localhost/server.php');

// Credentials
$client->authentication('jsonrpc', 'toto');

$result = $client->execute('addition', array('a' => 2, 'b' => 2));



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap