PitchHut logo
json-rpc
Seamless JSON-RPC implementation for PHP and JavaScript.
Pitch

This project provides a robust implementation of JSON-RPC, featuring a server built in PHP and a client in JavaScript. It streamlines communication between clients and servers by adhering to the 1.1 specification, allowing developers to create efficient and easy-to-use APIs with built-in error handling and promise support.

Description

The JSON-RPC project offers a robust implementation of the JSON-RPC protocol, facilitating seamless communication between PHP servers and JavaScript clients. This implementation adheres to version 1.1 of the JSON-RPC Specification, providing a reliable framework for remote procedure calls.

Server Implementation

The server component is designed in PHP, allowing developers to define methods that clients can invoke remotely. Below is a sample server implementation:

<?php
require('json-rpc.php');

class Foo {
    function ping($str) {
        return "pong '$str'";
    }
}

handle_json_rpc(new Foo());

?>

Client Implementation

The client component, implemented in JavaScript, enables interaction with the server-side methods. Here's how to set up the client to communicate with the server:

rpc({
    url: "foo.php",
    error: function(error) {
        alert(error.message);
    },
    debug: function(json, which) {
        console.log(which + ': ' + JSON.stringify(json));
    }
})(function(foo) {
    foo.ping("Hello")(function(response) {
        alert(response);
    });
});

For those who prefer using promises, the implementation supports that as well:

rpc({
    url: 'service.php',
    promisify: true
}).then(function(service) {
    service.ping("hello").then(function(response) {
       alert(response);
    });
});

Requirements

To utilize this JSON-RPC implementation, ensure that the mbstring PHP module is enabled in your environment.

This project simplifies the integration of server and client functionalities using the JSON-RPC protocol, paving the way for efficient communication between different programming environments.

0 comments

No comments yet.

Sign in to be the first to comment.