Zend_Rest_Server is intended as a fully-featured REST server.
Basic Zend_Rest_Server Usage - Classes
1 2 3 | $server = new Zend_Rest_Server();
$server->setClass('My_Service_Class');
$server->handle();
|
Basic Zend_Rest_Server Usage - Functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /**
* 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();
|
To call a Zend_Rest_Server service, you must supply a GET/POST method 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 arg following by the numeric position of the argument (i.e. “arg1”).
Note
Numeric index
Numeric arguments use a 1-based index.
To call sayHello from the example above, you can use either:
?method=sayHello&who=Davey&when=Day
or:
?method=sayHello&arg1=Davey&arg2=Day
When returning values, to return a custom status, you may return an array with a status key.
Returning Custom Status
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /**
* 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();
|
If you wish to return custom XML, simply return a DOMDocument, DOMElement or SimpleXMLElement object.
Return Custom XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /**
* 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();
|
The response from the service will be returned without modification to the client.
The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.