source code bean

07 Jan, 2010

Creating a JSON-RPC service using Zend Json Server

Posted by: Peter In: PHP| Web| Zend Framework

In this post I am going to show how easy it is to create a JSON-RPC web service using the built in support in Zend Framework.

First we need to create the php-file that will handle the incoming RPC calls. It is not advised to put this inside the MVC structure of a Zend web application, since that will lead to unessesary complexity and overhead. The Zend people recommend that we create the JSON-RPC under /public/api/vX/, so lets create the file /public/api/v1/jsonrpc.php (if you haven’t setup your Zend MVC structure, read my blog post Getting started with the zend framework to get started).

We will have to do the regular bootstrapping to get our application up and running:

  1.  
  2. // Define path to application directory
  3. defined(‘APPLICATION_PATH’)
  4.     || define(‘APPLICATION_PATH’, realpath(dirname(__FILE__) . ‘/../../../application’));
  5.  
  6. // Define application environment
  7. defined(‘APPLICATION_ENV’)
  8.     || define(‘APPLICATION_ENV’, (getenv(‘APPLICATION_ENV’) ? getenv(‘APPLICATION_ENV’) : ‘production’));
  9.  
  10. // Ensure library/ is on include_path
  11. set_include_path(implode(PATH_SEPARATOR, array(
  12.     realpath(‘../../../library’),
  13. )));
  14.  
  15. /** Zend_Application */
  16. require_once ‘Zend/Application.php’;
  17.  
  18. // Create application, bootstrap, and run
  19. $application = new Zend_Application(
  20.     APPLICATION_ENV,
  21.     APPLICATION_PATH . ‘/configs/application.ini’
  22. );
  23.  
  24. $application->bootstrap();
  25.  

The next step is to create the class that will be exposed through the service. I will create a very simple class that will simply perform an addition of two ints. It is very important to describe the input parameters using the @param directive in the comment. This information is used by the Json Server when creating the SMD (Service Mapping Description).

  1.  
  2. /**
  3.  * Simple – sample class to expose via JSON-RPC
  4.  */
  5. class Simple
  6. {
  7.     /**
  8.      * Return sum of two variables
  9.      *
  10.      * @param  int $x
  11.      * @param  int $y
  12.      * @return array
  13.      */
  14.     public function add($x, $y)
  15.     {
  16.         return array(‘result’ => $x + $y);
  17.     }
  18. }
  19.  

The last step to get the JSON-RPC server running:

  1.  
  2. // Instantiate server, etc.
  3. $server = new Zend_Json_Server();
  4. $server->setClass(‘Simple’);
  5.  
  6.  
  7. if (‘GET’ == $_SERVER[‘REQUEST_METHOD’]) {
  8.     // Indicate the URL endpoint, and the JSON-RPC version used:
  9.     $server->setTarget(‘/api/v1/jsonrpc.php’)
  10.            ->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2);
  11.  
  12.     // Grab the SMD
  13.     $smd = $server->getServiceMap();
  14.  
  15.     // Return the SMD to the client
  16.     header(‘Content-Type: application/json’);
  17.     echo $smd;
  18.     return;
  19. }
  20.  
  21. $server->handle();
  22.  

Now your JSON-RPC Server should be up running. Browsing http://{your web server}/api/v1/jsonrpc.php should result in the following SMD:

  1.  
  2. {
  3.   "transport":"POST",
  4.   "envelope":"JSON-RPC-2.0",
  5.   "contentType":"application\/json",
  6.   "SMDVersion":"2.0",
  7.   "target":"\/api\/v1\/jsonrpc.php",
  8.   "services": {
  9.       "add":{
  10.           "envelope":"JSON-RPC-2.0",
  11.           "transport":"POST",
  12.           "parameters":[
  13.               {"type":"integer","name":"x","optional":false},
  14.               {"type":"integer","name":"y","optional":false}],
  15.           "returns":"array"
  16.        }
  17.     },
  18.    "methods":{
  19.        "add":{
  20.            "envelope":"JSON-RPC-2.0",
  21.            "transport":"POST",
  22.            "parameters":[
  23.                {"type":"integer","name":"x","optional":false},
  24.                {"type":"integer","name":"y","optional":false}],
  25.            "returns":"array"}
  26.        }
  27. }
  28.  

jQuery does not support calling JSON-RPC services out of the box, but fourtenly there is plenty of plugins for jquery that fixes this. One is the JSON-RPC client found here. Download the client and put the javascript files into your /js folder. Then create a new file test.html and add the following html:

  1.  
  2. <html>
  3. <head>
  4.         <script LANGUAGE="javascript" SRC="js/jquery-1.3.min.js"></script>
  5.         <script LANGUAGE="javascript" SRC="js/json2.js"></script>
  6.         <script LANGUAGE="javascript" SRC="js/jquery.zend.jsonrpc.js"></script>
  7.         <script>
  8.                 $(document).ready(function(){
  9.                         test = jQuery.Zend.jsonrpc({url: ‘/api/v1/jsonrpc.php’});
  10.                         alert(test.add(1,1)['result']);
  11.                 });
  12.         </script>
  13. </head>
  14. <body>
  15. </body>
  16. </html>
  17.  

We are all done! Browsing test.html should result in an alert box containg the result.
result

Congratulations! You have created a JSON-RPC service!



Read more about the Zend Framework:

2 Responses to "Creating a JSON-RPC service using Zend Json Server"

1 | udara

May 3rd, 2010 at 3:08 am

Avatar

test.add(1,1)['result'],in this code u send two integer arguments to add() method.How to send a complicated object as a argument.
i mean
var ob=new Object();
ob.name=”udara”;
how can i send ob as a argument.
for the momnet im doing JSON.stringify(ob) andsent json string as a argument.

2 | Creating a JSON-RPC server with Zend Framework

May 21st, 2010 at 2:23 am

Avatar

[...] found this blog very helpful in figuring out how to do this. The first step is to create a new bootstrap file. I [...]

Comment Form

Categories

Adwords

Twitter Updates


    • Petan: I got in bootstrap this (insted of $frontController->getRouter()): $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini', 'rout
    • oanh tong ngoc: :) It's usefull but could U give everyone's an example with a project source code. Thanks
    • Peter: Hi Sohaib, It seems like the rewrite module isn't loaded by IIS. Have you uploaded the UrlRewriter dlls and made the changes to web.config on the ser

    About

    Welcome to source code bean! You will find information on tips and tricks on programming languages, server side stuff, and anything that causes troubles to web development.