Zend_Json_Server and how to call it via JSON-RPC 2.0

So I started playing with Zend_Json_Server and was having a hard time trying to figure out how to call the server from a client. Finally I checked the JSON-RPC 2.0 spec and read a very important detail that I had not realized:

The Request is expressed as a single JSON Object

This was the key to my problem. There are 4 parameters that can be sent with each JSON-RPC 2.0 request but I was sending each as a post var which is not what Zend_Json_Server expects. It simply wants one json encoded object with all the parameters inside. The 4 available parameters are:

  1. jsonrpc – the version you are using; I’m using 2.0.
  2. method – the name of the method you want to call in the server.
  3. params – object of parameters your method needs. If you don’t need any, don’t send this param.
  4. id – an identifier (anything you want) that will be sent to and from the server for this request.

I’m using Zend_Http_Client() to make the request and here is an example:

$params = array(
	'jsonrpc' => '2.0',
	'method' => 'find',
	'params' => array('326691'),
	'id' => 'test'
);

$http = new Zend_Http_Client();
$http->setUri('http://localhost/path/to/jsonrpc/server.php');
$http->setMethod(Zend_Http_Client::POST);
$http->setRawData(json_encode($params));

echo $http->request()->getBody();

Continue reading “Zend_Json_Server and how to call it via JSON-RPC 2.0”