Framework:sb Controller JSON RPC2

From Surebert wiki

Contents

Creating a JSON RPC2 Server Using sb_Controller_JSON_RPC2

Creatng a JSON RPC2 server with the surebert framework is easy. Simple extend the sb_Controller_JSON_RPC2_Server class with your own. This will allow you to select methods of you app which can then be called from another application.

php code

class CalculatorController extends sb_Controller_JSON_RPC2_Server{
 
	/**
	 * Adds two numbers together
	 * @param integer $x
	 * @param integer $y
	 * @return integer
	 * @servable true
	 */
	public function add($x, $y){
		return $x+$y;
	}
 
}

You could then call it with a client as such

php code

$client = new sb_JSON_RPC2_Client('http://yoursite.com/calculator');
$result = $client->add(1,2);
//$result would equal 3

HTTP Status Headers

By default a sb_JSON_RPC2_Server serves the appropriate HTTP status header with the response, see the table above. If you would like to suppress the headers from being sent set the controllers's surpress_http_status to true; You may have to do this for certain clients that won't read the message body if a status other than 200 is returned, e.g. 400, 404, 500

php code

public $suppress_http_status = true;

GZ Compression

A sb_JSON_RPC2_Server instance can serve the json data gzipped. This is an excellent way to save bandwith for larger data transfers. You can set the gz compression level by passing a level 0-9 to the $server->use_gz_encoding(3); method

php code

public $use_gz_encoding(6);

Use Encryption

sb_JSON_RPC2_Client and Server can encrypt communication using mcrypt. In order for it to work, both client and server must use the same key. javascript code

php code

//use encryption for transfer, pass the same key the server is using
public $use_encryption($key);