0

I am new for zend.i need to create web-service in zend using Zend_Json_Server with JSON responce. I have define api controller here..

<?php
class ApiController extends Zend_Controller_Action
{

    public function init()
    { }
    public function indexAction()
    { }
    public function restAction()
    {

      // disable layouts and renderers
          $this->getHelper('viewRenderer')->setNoRender ( true );

          // initialize REST server
          $server = new Zend_Json_Server();
          // set REST service class
          $server->setClass('Test_Return');

          // handle request
          if ('GET' == $_SERVER['REQUEST_METHOD']) {
               $server->setTarget('/json-rpc.php')
                       ->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2);
                $smd = $server->getServiceMap();

                // Set Dojo compatibility:
               // $smd->setDojoCompatible(true);

                header('Content-Type: application/json');
                echo $smd;

            }
          $server->handle();
    }
}
?>

And Test_Return define in Library/Test Test_Return code is here..

<?php
class Test_Return {

    public function add($x, $y)
    {
        return $x + $y;
    }
    public function subtract($x, $y)
    {
        return $x - $y;
    }
    public function multiply($x, $y)
    {
        return $x * $y;
    }
      public function divide($x, $y)
    {
        return $x / $y;
    }
} ?>

How can call particular expression.

1
  • I would expect the code of the server bootstrap to be in your index.php. I hope that this site [zendcasts.com/tag/rest/] can help you. I will have a look at it on the evenning Commented Feb 11, 2015 at 15:00

2 Answers 2

1

As describe in here on your index you create an instance of the zend_rest_server add your methods and run it. The methods should be specified in the url. I sugest you chose zend 2 for better implementation

Sign up to request clarification or add additional context in comments.

5 Comments

I have used this url but it throws error 'XML Parsing Error: XML or text declaration not at start of entity'
I have try using Zend_Json_Server .Can u suggest me how can call rest apis in the restfull method?.
{"error":{"code":-32600,"message":"Invalid Request","data":null},"id":null} error in json
The error mean that you are not passing any parameters. I understand that the method should be pass in the input method by default and the json on the data input. Maybe add a Content-Type="application/json" json on your request headers. It is very hard with out the code but i think the problem is where you make the request
0

The Zend_Json_Server initialization should be in your public/index.php

defined('APPLICATION_PATH') ||
    define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

defined('APPLICATION_ENV') ||
    define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

set_include_path(implode(PATH_SEPARATOR, array(
    dirname(dirname(__FILE__)) . '/libs',
    get_include_path(),
)));



require_once 'Zend/Application.php';

   $application = new Zend_Application(
      APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');


   $application->getBootstrap()->bootstrap();
   // Instantiate server ...
   $server = new Zend_Json_Server();
   include_once APPLICATION_PATH . '/Calculator.php';
   $server->setClass(new Calculator());
   if ('GET' == $_SERVER['REQUEST_METHOD'])
   {
        // Indicate the URL endpoint, and the JSON-RPC version used:
        $server->setTarget('/api/1.0/jsonrpc.php')->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2);
        // Grab the SMD
        $smd = $server->getServiceMap();
        // Return the SMD to the client
        header('Content-Type: application/json');
        echo $smd;
        return;
   }
   $server->handle();

$application->bootstrap()->run();

With curl on the command line you wont see anything ;-). This frustrated me a bit.

curl -H "Content-Type: application/json" -d '{"method":"add","x":5,"y":10}' http://zend.rest.server/api/1.0/jsonrpc.php

On the browser you can use this jQuery plugin

app = jQuery.Zend.jsonrpc({url: '/api/1.0/jsonrpc'});
app.add(5, 5);

{"result":10,"id":"1","jsonrpc":"2.0"}

You maybe want to follow the steps as describe here.

I suggest that you apgrade your version of zend if you can as zend2.X.X has better support for rest services.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.