Creating a Server
Two main classses, Zend_Rest_Client and Zend_Rest_Server, make up the Zend_Rest component. These classes provide the option to either create a REST client or a server that accepts REST requests. Before you dive into the client and call web services, you'll create your own server, which you'll later use to test the REST client implementations.
Looking under the hood, the Zend_Rest_Server class allows you to specify either a method or a complete class that contains the code to process requests. The Zend_Rest_Server methods, addFunction() and setClass(), provide such features (see Table 7-2). Let's take an example by creating a simple web service that returns a list of artists.
|
Operation |
Description |
|
_construct() |
Object constructor. |
|
handle() |
Specifies how to handle the request. Key-value arrays accepted in the parameter. |
|
addFunction() |
Specifies a function to add as a web server. Accepts a string value representing the |
|
function. | |
|
getFunctions() |
Returns a list of functions that have been fed into the web service using |
|
addFunction(). | |
|
getHeaders() |
Returns any extra headers the server has sent. |
|
returnResponse() |
Determines whether the web service should return a response. |
|
setClass() |
Specifies a class containing all web services. Accepts a string value representing the |
|
class name. | |
|
setPersistence() |
Sets the persistence mode (sets the way the server saves information between |
|
requests). |
The web service you will create can be invoked using the service name getartists and will return a SimpleXml object containing information for only one artist: the metal band Poison. To set up the server, you need to create a controller and a model. Create two files: ServicesController.php and Webservices.php. Place the ServicesController.php file inside your application/controllers directory and the WebServices.php file in the new application/models/services/ directory.
The controller will accept all incoming requests to the service URI /services/getartists, while the model will contain the domain logic.
The controller shown in Listing 7-1 is the primary service controller. Each action in the controller represents a service that can be invoked. The getartistsAction() method loads a single file: the file containing the domain logic the web service needs to process and produce a response for the request.
Listing 7-1. ServicesController.php
* ServiceController.php
* @author <Your name>, <Your e-email address>
class ServicesController extends Zend_Controller_Action {
public function indexAction(){}
* Display all the artists in the system.
public function getartistsAction() {
require_once "Services/WebServices.php";
$server = new Zend_Rest_Server(); $server->setClass('WebServices'); $server->handle(array('method' => 'getArtists'));
Once you load the required file, you create a Zend_Rest_Server object and identify the class that contains the web services WebServices using setClass(). Finally, you call the Zend_Rest_Server::handle() method, which accepts a single parameter as a key-value array. In this example, you pass in the method key, which identifies the method to call within the WebServices class. Let's take a look at this class now.
The model shown in Listing 7-2 contains only a single service. The getartists service returns a SimpleXML object that contains only a single artist. You create the well-formed XML string and pass the string into the PHP simplexml_load_string() function, which converts the XML string into a SimpleXML object. If you need to expand on this example and connect to a database, this is the appropriate place to put such code.
Listing 7-2. Webservices.php
* WebServices.php
* Containst full logic for web services.
class WebServices {
* Return a single artist.
* @return SimpleXML
public function getArtists() {
$xml = '<?xml version="1.0" standalone="yes"?><response>';
$xml .= '<artists><artist><name>Poison</name>';
$xml .= '<genre>Rock</genre></artist></artists>';
return simplexml_load_string($xml);
■ Note The name of the function does not have to be the same as the action in the web service controller. This was done for ease of use. To change the name of the class method, simply point the value in the method key to the new method you want to use.
You now have a service to test the client functionality. That wasn't hard, right? This wraps up the REST server functionality. Let's look at the client side of things. How do you make GET, POST, PUT, and DELETE calls using Zend Framework?
Post a comment