-- Werner <[EMAIL PROTECTED]> wrote
(on Thursday, 01 March 2007, 06:42 PM +0200):
> I also want to start playing around with it for an upcoming project.
> 
> Would you mind posting the skeleton on the list? (to me, the list also 
> serves as a repository (and archive) of code snippets, etc.
> 
> I love searching for keywords and topics in the list with my mail 
> client, it has proven to be a useful resource to me many times before. 
> Therefore, I would like to ask you to please put as much example code in 
> the list as possible, since you are knowledgeable and it provides a 
> general answer (or starting point) to list-subscribers.
> 
> Thank you for your time and effort,
> Werner
> 
> Matthew Weier O'Phinney wrote:
> >I have. Contact me off-list, and I'll provide a skeleton for you.

Okay, it's below.

There are some points where you'll need to modify it (require service
classes and attach them to the server, specify path to a cache file,
specify exception classes that may return XML-RPC faults, etc.).
Additionally, you may want to add some logging functionality surrounding
the handle() call -- log the request and response, for instance, or just
log request/response when fault responses occur.

=============================================================
<?php
// Turn of error reporting; mucks with XML responses
ini_set('display_errors', false);

/** Zend_Controller_Action */
require_once 'Zend/Controller/Action.php';

/** Zend_XmlRpc_Server */
require_once 'Zend/XmlRpc/Server.php';

/** Zend_XmlRpc_Server_Cache */
require_once 'Zend/XmlRpc/Server/Cache.php';

/** Zend_XmlRpc_Server_Fault */
require_once 'Zend/XmlRpc/Server/Fault.php';

/**
 * Also load your service classes here:
 * require_once 'Services/Foo.php'; // etc.
 */

/**
 * XMLRPC Controller: dispatch XMLRPC requests
 *
 * URI route is: /xml-rpc
 *
 * Any additional URI components will be ignored.
 * 
 * @uses Zend_Controller_Action
 * @uses Zend_XmlRpc_Server
 */
class XmlRpcController extends Zend_Controller_Action
{
    /**
     * Override and/or set this to specify a server cache file
     * @var string
     */
    public $cacheFile;

    /**
     * Array of exception classes that can be reported as XML-RPC faults. 
     * Override or set this value.
     * @var array
     */
    public $validExceptionClasses = array();

    /**
     * Sets the cache file to a sane default if not set
     * 
     * @return void
     */
    public function init()
    {
        if (null === $this->cacheFile) {
            $this->cacheFile = dirname(__FILE__) . '/../cache/xmlrpc.cache';
        }
    }

    /**
     * Index action: dispatch xmlrpc server
     * 
     * @return void
     */
    public function indexAction()
    {
        $this->dispatchXmlrpc();
    }

    /**
     * Forward to indexAction()
     * 
     * @param  string $method 
     * @param  array $args 
     * @return void
     */
    public function __call($method, $args)
    {
        return $this->_forward('index');
    }

    /**
     * Dispatch XMLRPC server
     * 
     * @access public
     * @return void
     */
    public function dispatchXmlrpc()
    {
        $server = new Zend_XmlRpc_Server();

        // Ortus_Exceptions will be returned as fault responses
        foreach ($this->validExceptionClasses as $class) {
            Zend_XmlRpc_Server_Fault::attachFaultException($class);
        }

        if (!Zend_XmlRpc_Server_Cache::get($this->cacheFile, $server)) {
            /**
             * Attach XML-RPC classes, with optional namespaces. Example:
             * $server->setClass('Service_Foo', 'foo');
             * $server->setClass('Model_Bar', 'bar');
             */

            Zend_XmlRpc_Server_Cache::save($this->cacheFile, $server);
        }

        // Dispatch request...
        $response = $server->handle();
        $response->setEncoding('ISO-8859-1');
        header('Content-Type: text/xml; charset=ISO-8859-1');
        echo $response->saveXml();
    }
}

-- 
Matthew Weier O'Phinney
PHP Developer            | [EMAIL PROTECTED]
Zend - The PHP Company   | http://www.zend.com/

Reply via email to