Giter Site home page Giter Site logo

jsonrpc's Introduction

JsonRPC PHP Client and Server

A simple Json-RPC client/server that just works.

Features

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

Requirements

  • The only dependency is the cURL extension
  • PHP >= 5.3

Author

Frédéric Guillot

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.

Example with shortcut methods:

<?php

require 'JsonRPC/Client.php';

use JsonRPC\Client;

$client = new Client('http://localhost/server.php');
$result = $client->random([50, 100]);

var_dump($result);

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 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));

jsonrpc's People

Contributors

fguillot avatar speachy avatar s4l1h avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.