PHP and JSON-RPC

JSON

JSON (JavaScript Object Notation) is a nice solution for the people who needs interoperation with low costs, especially in parsing time. The problem is that you have to specify your interoperation rules manually, and sometimes that is not appreciated by your service’s consumers. I can hear them saying “Don’t waste your time! use SOAP or XML-RPC like everyone else”, and that what your are doing, not wasting parsing time, yes you are right, you know that, but they don’t care, their answer is “who cares?, buy more processor or more memory, but don’t waste my developer’s time on your hand made services”. So, what you need is a lightweight remote procedure call protocol similar to XML-RPC but in JSON format, and then provide them of already implemented object to interoperate with your RPCed JSON service.

The first answer to your first question is yes, already exist such thing, and is named JSON-RPC, very creative name isn’t it? I’m sure you don’t see that coming ;-), and you can find the project at http://json-rpc.org/.

Principal features?

– Inspired in the XML-RPC protocol.
– Request and Response are serialized in JSON.
– Data transported via HTTP.
– Very often used asynchronously, to avoid frozen applications while request are in process.

How it works?

Request. The request is a single object with the following properties:
method. A string containing the name.
params. An array of parameters.
id. A request’s unique identifier.

Response. The response is a single object with the following properties:
result. The result of the method call.
error. An error object. If occurred.
id. The request’s unique identifier.

In JSON code:

JSON-RPC Request and Response example

OK this post’s title and category are about PHP, so the question is: There is a PHP implementation of the JSON-RPC project? and the answer is yes, there are more than one, so here is the list of recomendations:

PHP-O-Lait
JSON-RPC PHP
JSON/XML-RPC Client and Server Implementations

Basilio Briceño

DevOps evangelist, SoftwareLibre activist, sometimes speaker & eclectic metalhead.

1 comment

  1. Alin   •     Author

    Very useful information

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>