<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.rest.server">
    <title>Zend_Rest_Server</title>

    <sect2 id="zend.rest.server.introduction">
        <title>Introduction</title>

        <para>
            <classname>Zend_Rest_Server</classname> is intended as a fully-featured REST server.
        </para>
    </sect2>

    <sect2 id="zend.rest.server.usage">
        <title>REST Server Usage</title>

        <example id="zend.rest.server.usage.example-1">
            <title>Basic Zend_Rest_Server Usage - Classes</title>

            <programlisting language="php"><![CDATA[
$server = new Zend_Rest_Server();
$server->setClass('My_Service_Class');
$server->handle();
]]></programlisting>
        </example>

        <example id="zend.rest.server.usage.example-2">
            <title>Basic Zend_Rest_Server Usage - Functions</title>

            <programlisting language="php"><![CDATA[
/**
 * 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();
]]></programlisting>
        </example>
    </sect2>

    <sect2 id="zend.rest.server.args">
        <title>Calling a Zend_Rest_Server Service</title>

        <para>
            To call a <classname>Zend_Rest_Server</classname> service, you must supply a
            <constant>GET</constant>/POST <code>method</code> argument with a value that is the
            method you wish to call. You can then follow that up with any number
            of arguments using either the name of the argument (i.e. "who") or
            using <code>arg</code> following by the numeric position of the
            argument (i.e. "arg1").
        </para>

        <note>
            <title>Numeric index</title>

            <para>
                Numeric arguments use a 1-based index.
            </para>
        </note>

        <para>
            To call <code>sayHello</code> from the example above, you can use either:
        </para>

        <para>
            <code>?method=sayHello&amp;who=Davey&amp;when=Day</code>
        </para>

        <para>
            or:
        </para>

        <para>
            <code>?method=sayHello&amp;arg1=Davey&amp;arg2=Day</code>
        </para>
    </sect2>

    <sect2 id="zend.rest.server.customstatus">
        <title>Sending A Custom Status</title>

        <para>
            When returning values, to return a custom status, you may return an
            array with a <code>status</code> key.
        </para>

        <example id="zend.rest.server.customstatus.example-1">
            <title>Returning Custom Status</title>

            <programlisting language="php"><![CDATA[
/**
 * Say Hello
 *
 * @param string $who
 * @param string $when
 * @return array
 */
function sayHello($who, $when)
{
    return array('msg' => "An Error Occurred", 'status' => false);
}

$server = new Zend_Rest_Server();
$server->addFunction('sayHello');
$server->handle();
]]></programlisting>
        </example>
    </sect2>

    <sect2 id="zend.rest.server.customxml">
        <title>Returning Custom XML Responses</title>

        <para>
            If you wish to return custom <acronym>XML</acronym>, simply return a
            <code>DOMDocument</code>, <code>DOMElement</code> or
            <code>SimpleXMLElement</code> object.
        </para>

        <example id="zend.rest.server.customxml.example-1">
            <title>Return Custom XML</title>

            <programlisting language="php"><![CDATA[
/**
 * Say Hello
 *
 * @param string $who
 * @param string $when
 * @return SimpleXMLElement
 */
function sayHello($who, $when)
{
    $xml ='<?xml version="1.0" encoding="ISO-8859-1"?>
<mysite>
    <value>Hey $who! Hope you\'re having a good $when</value>
    <code>200</code>
</mysite>';

    $xml = simplexml_load_string($xml);
    return $xml;
}

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

$server->handle();
]]></programlisting>
        </example>

        <para>
            The response from the service will be returned without modification
            to the client.
        </para>
    </sect2>
</sect1>
<!--
vim:se ts=4 sw=4 et:
-->
