Programmer's Reference Guide
| Zend_Soap |
Zend_Soap_Server
Zend_Soap_Server class is intended to simplify Web Services server part development for PHP programmers.
It may be used in WSDL or non-WSDL mode, and using classes or functions to define Web Service API.
When Zend_Soap_Server component works in the WSDL mode, it uses already prepared WSDL document to define server object behavior and transport layer options.
WSDL document may be auto-generated with functionality provided by Zend_Soap_AutoDiscovery component or should be constructed manually using Zend_Soap_Wsdl class or any other XML generating tool.
If the non-WSDL mode is used, then all protocol options have to be set using options mechanism.
Zend_Soap_Server constructor
Zend_Soap_Server constructor should be used a bit differently for WSDL and non-WSDL modes.
Zend_Soap_Server constructor for the WSDL mode
Zend_Soap_Server constructor takes two optional parameters when it works in WSDL mode:
-
$wsdl, which is an URI of a WSDL file [1] setWsdl($wsdl) .
-
$options - options to create SOAP server object [2] setOptions($options) .
The following options are recognized in the WSDL mode:
-
'soap_version' ('soapVersion') - soap version to use (SOAP_1_1 or SOAP_1_2).
-
'actor' - the actor URI for the server.
-
'classmap' ('classMap') which can be used to map some WSDL types to PHP classes.
The option must be an array with WSDL types as keys and names of PHP classes as values.
-
'encoding' - internal character encoding (UTF-8 is always used as an external encoding).
-
'wsdl' which is equivalent to setWsdl($wsdlValue) call.
-
Zend_Soap_Server constructor for the non-WSDL mode
The first constructor parameter must be set to NULL if you plan to use Zend_Soap_Server functionality in non-WSDL mode.
You also have to set 'uri' option in this case (see below).
The second constructor parameter ($options) is an array with options to create SOAP server object [3] setOptions($options) .
The following options are recognized in the non-WSDL mode:
-
'soap_version' ('soapVersion') - soap version to use (SOAP_1_1 or SOAP_1_2).
-
'actor' - the actor URI for the server.
-
'classmap' ('classMap') which can be used to map some WSDL types to PHP classes.
The option must be an array with WSDL types as keys and names of PHP classes as values.
-
'encoding' - internal character encoding (UTF-8 is always used as an external encoding).
-
'uri' (required) - URI namespace for SOAP server.
Methods to define Web Service API
There are two ways to define Web Service API when your want to give access to your PHP code through SOAP.
The first one is to attach some class to the Zend_Soap_Server object which has to completely describe Web Service API:
- ...
- class MyClass {
- /**
- * This method takes ...
- *
- * @param integer $inputParam
- * @return string
- */
- public function method1($inputParam) {
- ...
- }
- /**
- * This method takes ...
- *
- * @param integer $inputParam1
- * @param string $inputParam2
- * @return float
- */
- public function method2($inputParam1, $inputParam2) {
- ...
- }
- ...
- }
- ...
- $server = new Zend_Soap_Server(null, $options);
- // Bind Class to Soap Server
- $server->setClass('MyClass');
- // Bind already initialized object to Soap Server
- $server->setObject(new MyClass());
- ...
- $server->handle();
Note: Important!
You should completely describe each method using method docblock if you plan to use autodiscover functionality to prepare corresponding Web Service WSDL.
The second method of defining Web Service API is using set of functions and addFunction() or loadFunctions() methods:
- ...
- /**
- * This function ...
- *
- * @param integer $inputParam
- * @return string
- */
- function function1($inputParam) {
- ...
- }
- /**
- * This function ...
- *
- * @param integer $inputParam1
- * @param string $inputParam2
- * @return float
- */
- function function2($inputParam1, $inputParam2) {
- ...
- }
- ...
- $server = new Zend_Soap_Server(null, $options);
- $server->addFunction('function1');
- $server->addFunction('function2');
- ...
- $server->handle();
Request and response objects handling
Note: Advanced
This section describes advanced request/response processing options and may be skipped.
Zend_Soap_Server component performs request/response processing automatically, but allows to catch it and do some pre- and post-processing.
Request processing
Zend_Soap_Server::handle() method takes request from the standard input stream ('php://input'). It may be overridden either by supplying optional parameter to the handle() method or by setting request using setRequest() method:
- ...
- $server = new Zend_Soap_Server(...);
- ...
- // Set request using optional $request parameter
- $server->handle($request);
- ...
- // Set request using setRequest() method
- $server->setRequest();
- $server->handle();
Request object may be represented using any of the following:
-
DOMDocument (casted to XML)
-
DOMNode (owner document is grabbed and casted to XML)
-
SimpleXMLElement (casted to XML)
-
stdClass (__toString() is called and verified to be valid XML)
-
string (verified to be valid XML)
Last processed request may be retrieved using getLastRequest() method as an XML string:
- ...
- $server = new Zend_Soap_Server(...);
- ...
- $server->handle();
- $request = $server->getLastRequest();
Response pre-processing
Zend_Soap_Server::handle() method automatically emits generated response to the output stream. It may be blocked using setReturnResponse() with TRUE or FALSE as a parameter [4] setReturnResponse() . Generated response is returned by handle() method in this case.
- ...
- $server = new Zend_Soap_Server(...);
- ...
- // Get a response as a return value of handle() method
- // instead of emitting it to the standard output
- $server->setReturnResponse(true);
- ...
- $response = $server->handle();
- ...
Last response may be also retrieved by getLastResponse() method for some post-processing:
- ...
- $server = new Zend_Soap_Server(...);
- ...
- $server->handle();
- $response = $server->getLastResponse();
- ...
| Zend_Soap |
Add A Comment
Please do not report issues via comments; use the ZF Issue Tracker.
If you have a JIRA/Crowd account, we suggest you login first before commenting.

Comments
How can I access the headers in the soap-reauest, so I may use them in the handling class?
I'm thinking of the content of the <soapenv:Header> tag inside the root <soapenv:Envelope> tag of the reuest, not the HTTP-request headers obviously.
This component proposal:
http://framework.zend.com/wiki/pages/viewpage.action?pageId=20109
even mentions this ability, and has a usecase using a server->setReadHeaders(true), but none of this exists in the current 1.10 version, or am I severely mistaken?
http://php.net/manual/en/book.soap.php
Ryan
22-Oct-2008 01:21
If you are having an issue where SOAP cannot find the functions that are actually there if you view the wsdl file, it's because PHP is caching the wsdl file (for a day at a time). To turn this off, have this line on every script that uses SOAP: ini_set("soap.wsdl_cache_enabled", "0"); to disable the caching feature.
Although it might sounds obvious it might be worth noting that when you make your Soap Server accessible through a controller you should disable your layout to make sure that your Soap Server only responds with a Soap Message.
For example, if I create a client as follows, it returns results as expected.
$soap_url = 'http://mydomainhere.com/soap/?wsdl';
$soap_client = new SoapClient($soap_url);
$result = $soap_client->method();
var_dump($result);
IF I attempt this:
$soap_url = 'https://mydomainhere.com/soap/?wsdl';
$soap_client = new SoapClient($soap_url);
$result = $soap_client->method();
var_dump($result);
(Note the 'https' in the soap url.
... then it fails with the exception:
[faultstring] => Could not connect to host
[faultcode] => HTTP
I have verified that I can browse to "https://mydomainhere.com/soap/?wsdl".
Do I need to add anything to handle incoming requests over ssl?
Petit bug dans cette classe, je m'explique, j'ai développé un client SOAP j'utilise la classe Zend ini pour pouvoir modifier les différents paramêtres pour le SOAP, simplement si vous mettez des masque de bit (bitmask) en l'occurence ceux du SOAP : SOAP_SINGLE_ELEMENT_ARRAYS, SOAP_USE_XSI_ARRAY_TYPE et, SOAP_WAIT_ONE_WAY_CALLS qui sont les features du SOAP cela ne marche pas il interprete surment les faite que se soit une string... il faut le faire en dur de cette façon : setOption(array('features' => SOAP_SINGLE_ELEMENT_ARRAYS)) cette méthode marche.
a plus !