Interview Questions

PHP SOAP Extension - A First SOAP Server

SOAP Interview Questions and Answers


(Continued from previous question...)

PHP SOAP Extension - A First SOAP Server

Let's try to write our own SOAP Web service that will do the same as the XMethods “Delayed Stock Quote” service.

The first task is to create a WSDL document describing our service in a format that client requests will understand. This requires minor modifications to the original document taken from the Xmethods site, so we'll start by taking a close look at that file.

The message section defines two messages. The first is getQuoteRequest, which is a request to relay the getQuote message and takes one string parameter called symbol. The other is getQuoteResponse, which is a response to the getQuote message, containing one float value, named Result.

The portType section defines one operation, getQuote, which describes which of the messages listed in the message section will be used to transmit the request and response.

The binding section defines how the messages must be transmitted and encoded. Here it tells us that we will be sending an RPC request using SOAP encoding across HTTP. It also specifies namespace and value of the SOAPAction header for the getQuote method.

Lastly, the service section defines the endpoint URL where the service is running.

Example (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("stockquote.wsdl");
$server->addFunction("getQuote");
$server->handle();
?>

(Continued on next question...)

Other Interview Questions