views:

416

answers:

1

How would I create an API for my web application? I am looking into Zend_Rest but I cannot grasp the idea behind it.

Basically, I would want something like this example:

$technorati = new Zend_Rest_Client('http://api.technorati.com/bloginfo');
$technorati->key($key);
$technorati->url('http://pixelated-dreams.com');
$result = $technorati->get();
echo $result->firstname() .' '. $result->lastname();

But I am totally lost on how to implement it using Zend Framework. Would I need to use Zend_Rest_Server? Any form of guidance on how to do this would be greatly appreciated...

A: 

What's the problem with the Zend Framework documentation on Zend_Rest_Server? There is an absolutely minimalistic example on how to use Zend_Rest_Server:

/**
 * Say Hello
 *
 * @param string $who
 * @param string $when
 * @return string
 */
function sayHello($who, $when)
{
    return "Hello $who, Good $when";
}

$server = new Zend_Rest_Server();
$server->addFunction('sayHello');
$server->handle();

Or do you have any specific problem?

EDIT: Regarding your question about the MVC integration I think, that this would introduce an extrem overhead together with a lot of functionality you don't need for webservices. Nevertheless I should be possible to integrate the service in to the MVC without any major problems (barring the overhead).

class Hello
{
    /**
     * Say Hello
     *
     * @param string $who
     * @param string $when
     * @return string
     */
    function sayHello($who, $when)
    {
        return "Hello $who, Good $when";
    }
}

class ApiController extends Zend_Controller_Action
{
    /**
     * @var Zend_Rest_Server
     */
    protected $_server;

    public function init()
    {
        // disable view rendering
        $this->_helper->viewRenderer->setNeverRender(true);
        // disable layout (if you use layouts)
        $this->_helper->layout->disableLayout();

        $this->_server = new Zend_Rest_Server();
        $this->_server->setClass('Hello');
    }

    public function __call($method, $args)
    {
        $params = $this->_getAllParams();
        $params['method'] = $method;
        $this->_server->handle($params);
    }
}

/*
 * This should allow you to call http://www.example.com/api/sayHello/who/MyName/when/morning
 * By defining custom routes you're able to control the urls.
 */

Please be aware that the code is untested and surely needs some tweaking especially regarding the error handling but it can serve as a simple MVC service example.

Stefan Gehrig
I could not find a good example on how to use that in MVC. Can you show me how to do that within a Controller?
wenbert
Thank you for the example. I wanted something like this because I want to use "parts" of Zend Framework to query my database, etc. I would upvote if I had enough reputation :) Really appreciate the reply :D
wenbert
JUst to make that clear: you don't have to use the MVC just to use the other Zend Framework components. In that way the Zend Framework is not as strict as other PHP frameworks - it's merely a collection of components to be used at will (with not too many inter-component dependencies).
Stefan Gehrig