Zend_Rest_Client | Zend_Rest Programmer's Reference Guide | Zend_Search_Lucene

# Zend_Rest_Server

# Introduction

Zend_Rest_Server is intended as a fully-featured REST server.

# REST Server Usage

Example #1 Basic Zend_Rest_Server Usage - Classes

1. $server = new Zend_Rest_Server();
2. $server->setClass('My_Service_Class');
3. $server->handle();

Example #2 Basic Zend_Rest_Server Usage - Functions

1. /**
2. * Say Hello
3. *
4. * @param string $who
5. * @param string $when
6. * @return string
7. */
8. function sayHello($who, $when)
9. {
10. return "Hello $who, Good $when";
11. }
13. $server = new Zend_Rest_Server();
14. $server->addFunction('sayHello');
15. $server->handle();

# Calling a Zend_Rest_Server Service

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`

# Sending A Custom Status

When returning values, to return a custom status, you may return an array with a `status` key.

Example #3 Returning Custom Status

1. /**
2. * Say Hello
3. *
4. * @param string $who
5. * @param string $when
6. * @return array
7. */
8. function sayHello($who, $when)
9. {
10. return array ('msg' => "An Error Occurred", 'status' => false);
11. }
13. $server = new Zend_Rest_Server();
14. $server->addFunction('sayHello');
15. $server->handle();

# Returning Custom XML Responses

If you wish to return custom XML, simply return a `DOMDocument`, `DOMElement` or `SimpleXMLElement` object.

Example #4 Return Custom XML

1. /**
2. * Say Hello
3. *
4. * @param string $who
5. * @param string $when
6. * @return SimpleXMLElement
7. */
8. function sayHello($who, $when)
9. {
10. $xml ='<?xml version="1.0" encoding="ISO-8859-1"?>
11. <mysite>
12. <value>Hey $who! Hope you\'re having a good $when</value>
13. <code>200</code>
14. </mysite>';
16. $xml = simplexml_load_string($xml);
17. return $xml;
18. }
20. $server = new Zend_Rest_Server();
21. $server->addFunction('sayHello');
23. $server->handle();

The response from the service will be returned without modification to the client.

Zend_Rest_Client | Zend_Rest Programmer's Reference Guide | Zend_Search_Lucene