1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
<?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&who=Davey&when=Day</code>
</para>
<para>
or:
</para>
<para>
<code>?method=sayHello&arg1=Davey&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:
-->
|