With the Zend framework you can choose between Rest-, XmlRpc- and Soap-Webservices. I am going to describe here all of them with a simple ping service.
For all of them you need first a function or a class which you like to expose as service.
MyTestService.php
<?php
class MyTestService {
/**
* A simple ping service
*
* @param string $value
* @return string
*/
function ping($value) {
return $value . ' back from server';
}
}
It is quite important to add the documentation with the exact datatype for all parameters as well as for the return value to the function. This information is used by the Zend framework to parse the values e.g. for the WSDL generation.
Rest Service
RestService.php
<?php
require 'Zend/Rest/Server.php';
require_once 'MyTestService.php';
$server = new Zend_Rest_Server();
$server->setClass('MyTestService');
$server->handle();
XmlRpc Service
XmlRpcService.php
<?php
require 'Zend/XmlRpc/Server.php';
require_once 'MyTestService.php';
$server = new Zend_XmlRpc_Server();
$server->setClass('MyTestService');
echo $server->handle();
Caution: the echo on the last line is here essential!
Soap Service
SoapWebService.php
<?php
require 'MyTestService.php';
if (isset ( $_GET ['wsdl'] )) {
require 'Zend/Soap/AutoDiscover.php';
$autodiscover = new Zend_Soap_AutoDiscover ( );
$autodiscover->setClass ( 'MyTestService' );
$autodiscover->handle ();
} else {
require 'Zend/Soap/Server.php';
$url = URL;
$server = new Zend_Soap_Server ( "http://localhost/SoapWebService.php?wsdl" );
$server->setClass ( 'MyTestService' );
echo $server->handle ();
}
What we can see here is the possibility to generate the WSDL file automaticaly. The class Zend_Soap_AutoDiscover is doing this for us. We have just to set the class that we use as service. The WSDL is now accessible under the URL http://localhost/SoapWebService.php?wsdl.
Client
Accessing the services is as easy as creating them.
index.php
<?php
require 'Zend/Rest/Client.php';
require 'Zend/XmlRpc/Client.php';
require 'Zend/Soap/Client.php';
try {
// Rest
$client = new Zend_Rest_Client ( "http://localhost/RestService.php" );
$result = $client->ping ( 'test' )->get ();
echo 'Rest:<br/>';
var_dump ( $result );
// XmlRpc
$client = new Zend_XmlRpc_Client ( "http://localhost/XmlRpcService.php" );
$result= $client->call('ping', array('test'));
echo '<br/><br/>XmlRpc:<br/>';
var_dump ( $result );
// Soap
$client = new Zend_Soap_Client ( "http://localhost/SoapService.php?wsdl" );
$result = $client->ping ( 'test' );
echo '<br/><br/>Soap:<br/>';
var_dump ( $result );
} catch ( Exception $e ) {
echo '<b style="color:#FF0000;">Error: ' . $e->getMessage () . '</b>';
}
As you can see, the Rest- and the Soap service are a bit more intuitive to use than the XmlRpc service. Which one is best depends on the requirements and the use of the service.
With the Zend framework you can choose between Rest-, XmlRpc- and Soap-Webservices. I am going to describe here all of them with a simple ping service.
For all of them you need first a function or a class which you like to expose as service.
MyTestService.php
<?php
class MyTestService {
/**
* A simple ping service
*
* @param string $value
* @return string
*/
function ping($value){
return $value .' back from server';
}
}
It is quite important to add the documentation with the exact datatype for all parameters as well as for the return value to the function. This information is used by the Zend framework to parse the values e.g. for the WSDL generation.
Rest Service
RestService.php
<?php
require 'Zend/Rest/Server.php';
require_once 'MyTestService.php';
$server = new Zend_Rest_Server();
$server->setClass('MyTestService');
$server->handle();
XmlRpc Service
XmlRpcService.php
<?php
require 'Zend/XmlRpc/Server.php';
require_once 'MyTestService.php';
$server = new Zend_XmlRpc_Server();
$server->setClass('MyTestService');
echo $server->handle();
Caution: the echo on the last line is here essential!
Soap Service
SoapWebService.php
<?php
require 'MyTestService.php';
if(isset($_GET['wsdl'])) {
require 'Zend/Soap/AutoDiscover.php';
$autodiscover = new Zend_Soap_AutoDiscover();
$autodiscover->setClass('MyTestService');
$autodiscover->handle();
} else {
require 'Zend/Soap/Server.php';
$url = URL;
$server = new Zend_Soap_Server("http://localhost/SoapWebService.php?wsdl");
$server->setClass('MyTestService');
echo $server->handle();
}
What we can see here is the possibility to generate the WSDL file automaticaly. The class Zend_Soap_AutoDiscover is doing this for us. We have just to set the class that we use as service. The WSDL is now accessible under the URL http://localhost/SoapWebService.php?wsdl.
Client
Accessing the services is as easy as creating them.
index.php
<?php
require 'Zend/Rest/Client.php';
require 'Zend/XmlRpc/Client.php';
require 'Zend/Soap/Client.php';
try {
// Rest
$client = new Zend_Rest_Client ( "http://localhost/RestService.php" );
$result = $client->ping('test')->get();
echo 'Rest:<br/>';
var_dump($result);
// XmlRpc
$client = new Zend_XmlRpc_Client ( "http://localhost/XmlRpcService.php" );
$result= $client->call(''ping'', array('test'));
echo '<br/><br/>XmlRpc:<br/>';
var_dump($result);
// Soap
$client = new Zend_Soap_Client("http://localhost/SoapService.php?wsdl");
$result = $client->ping('test');
echo '<br/><br/>Soap:<br/>';
var_dump($result);
} catch ( Exception $e ) {
echo '<b style="color:#FF0000;">Error: ' . $e->getMessage () . '</b>';
}
As you can see, the Rest- and the Soap service are a bit more intuitive to use than the XmlRpc service. Which one is best depends on the requirements and the use of the service.