Home arrow Zend arrow Page 5 - PHP SOAP Extension

Creating the Server - Zend

This article describes the new SOAP extension for PHP. It is intended for PHP developers who want to write their own Web Services servers, or use SOAP to access existing ones. It assumes some familiarity with Web Services, SOAP, and WSDL (Web Services Description Language).

TABLE OF CONTENTS:
  1. PHP SOAP Extension
  2. A First SOAP Client
  3. Using WSDL
  4. A First SOAP Server
  5. Creating the Server
  6. Example 6
  7. Example 8 (client5.php)
  8. Other Implementations of SOAP for PHP
By: Zend
Rating: starstarstarstarstar / 23
March 17, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Now it’s time to create our server.
First, we’ll implement the getQuote() function, which will be accessed as a service function by incoming request messages from the web. Next, we’ll create a SoapServer object and connect it with the service function using SoapServer::addFunction() method. As you will see, the SoapServer() constructor has only one parameter: the path of the WSDL document that describes the service.

Example 4 (server1. php)


<?php
$quotes 
= array(
  
"ibm" => 98.42
);  
function 
getQuote($symbol) {
  
global $quotes;
  
return $quotes[$symbol];
}
ini_set("soap.wsdl_cache_enabled""0"); // disabling WSDL cache
$server = new SoapServer("stockquote1.wsdl");
$server->addFunction("getQuote");
$server->handle();




The SoapServer can work without a WSDL document in much the same way that the SoapClient can, but there are no obvious benefits to be had from setting it up in this way. Were you to do so, you should ensure that the return values are special objects of the SoapParam and SoapVar classes (as in the first example).

Here is a client for accessing our own SOAP server. Nothing has changed from the previous example except the WSDL location. It assumes that “stockquote1.wsdl” is in the same directory as our SOAP server.

Example 5 (client3.php)


<?php
  $client 
= new SoapClient("stockquote1.wsdl");
  
print($client->getQuote("ibm"));? >


What are the problem areas with our server and client?

To start with, they don’t handle errors. What happens when the server doesn’t recognize the requested symbol?  The SOAP protocol specifies a special format of messages for reporting errors – SoapFault. To generate such messages the server should throw an exception using the SoapFault object. The first parameter to the SoapFault() constructor is a fault code string, and the second is a fault description string. The client should be written in such a way as to catch SoapFault exceptions.

Secondly, it would be better to encapsulate Web Service functionality in a PHP class. In this case we wouldn’t need to use global variables and add each SOAP method to the server individually; we could add an entire class, and all its methods would be accessible through SOAP.



 
 
>>> More Zend Articles          >>> More By Zend
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

ZEND ARTICLES

- Taking the Zend Certified PHP Engineer Exam:...
- Quick Introduction to PHP 5
- PHP SOAP Extension
- Improving Performance
- PDFs with PHP part 2
- PDFs with PHP part 1
- PHP at Lycos
- Build Database Interfaces

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: