Home arrow Zend arrow Page 6 - PHP SOAP Extension

Example 6 - 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

A modified version of our SOAP server and client follows:

Example 6 (server2.php)


<?php
class QuoteService 
{
  
private $quotes = array("ibm" => 98.42);  
  function 
getQuote($symbol) {
    
if (isset($this->quotes[$symbol])) {
      
return $this->quotes[$symbol];
    
} else {
      
throw new SoapFault("Server","Unknown Symbol '$symbol'.");
    
}
  
}
}
$server = new SoapServer("stockquote2.wsdl");
$server
->setClass("QuoteService");
$server
->handle();
? >

As you can see, I have used the SoapServer::setClass() method to connect the SoapServer object with the QuoteService class.

Example 7 (client4.php)


<?php
  $client 
= new SoapClient("stockquote2.wsdl");
  
try {
    
echo "<pre>n";
    
print($client->getQuote("ibm"));
    
echo "n";
    
print($client->getQuote("microsoft"));  
    
echo "n</pre>n";
  
} catch (SoapFault $exception) {
    
echo $exception;   
  
}



What’s inside?
Are you curious about the SOAP message format, or hoping to debug a SOAP client of your own? If so, this section is for you.
The SoapClient() constructor accepts an associative array as its second parameter, as you already saw in the first example. Various options can be passed through this associative array. Here are just two:
  • trace – allows the client to store SOAP requests and responses (turned off by default)
  • exceptions – allows the client to control the exception mechanism (turned on by default)
Take a look at the following SOAP client example. It is derived from example 5, and shows precisely what is transmitted between the client and the server. In order to retrieve this information we use the SoapClient methods __getLastRequest() and __getLastResponse().



 
 
>>> 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: