Web Services with SOAP in PHP

W

The SOAP and XML-RPC extensions are packaged with the PHP 5 installation. The SOAP extension and the XML-RPC extension are not enabled by default in a PHP installation. To enable the SOAP and XML-RPC extensions add the following extension directives in the php.ini configuration file.

extension=php_xmlrpc.dll
extension=php_soap.dll

Restart the Apache server to activate the SOAP and XML-RPC extensions.

client.php
<?php
ini_set( ‘soap.wsdl_cache_enable’ , 0 );
ini_set( ‘soap.wsdl_cache_ttl’ , 0 );

$client = new SoapClient( ‘stockquote.wsdl’,
array(
‘trace’         => 1,
‘exceptions’   => 1,
‘style’         => SOAP_DOCUMENT,
‘use’         => SOAP_LITERAL,
‘soap_version’   => SOAP_1_1,
‘encoding’      => ‘UTF-8’
)
);

echo $client->getQuote( ‘nano’ ); //92.32

?>

Server1.php

<?php
$quotes = array( ‘nano’ => 92.32 );

function getQuote( $symbol ){
global $quotes;
return $quotes[ $symbol ];
}

ini_set( ‘soap.wsdl_cache_enable’ , 0 );
ini_set( ‘soap.wsdl_cache_ttl’ , 0 );

$server   = new SoapServer(
‘stockquote.wsdl’,
array(
‘exceptions’   => 1,
‘style’         => SOAP_DOCUMENT,
‘use’         => SOAP_LITERAL,
‘soap_version’   => SOAP_1_2,
‘encoding’      => ‘UTF-8’
)
);

$server->addFunction( ‘getQuote’ );
$server->handle();
?>

stockquote.wsdl
<?xml version =”1.0″ encoding =”UTF-8″ ?>
<wsdl:definitions
name=”StockQuote”
xmlns:tns=”urn:StockQuote”
xmlns:soap=”http://schemas.xmlsoap.org/wsdl/soap/”
xmlns:wsdl=”http://schemas.xmlsoap.org/wsdl/”
xmlns:xsd=”http://www.w3.org/2001/XMLSchema”
targetNamespace=”urn:StockQuote”>

<wsdl:types>
<xsd:schema targetNamespace=”urn:StockQuote”>
<xsd:element name=”symbol” type=”xsd:string” />
<xsd:element name=”Result” type=”xsd:float” />
</xsd:schema>
</wsdl:types>

<wsdl:message name=”getQuoteRequest”>
<wsdl:part element=”tns:symbol” name=”symbol” />
</wsdl:message>
<wsdl:message name=”getQuoteResponse”>
<wsdl:part element=”tns:Result” name=”Result” />
</wsdl:message>

<wsdl:portType name=”StockQuotePortType”>
<wsdl:operation name=”getQuote”>
<wsdl:input message=”tns:getQuoteRequest” />
<wsdl:output message=”tns:getQuoteResponse” />
</wsdl:operation>
</wsdl:portType>

<wsdl:binding name=”StockQuoteBinding” type=”tns:StockQuotePortType”>
<soap:binding style=”document” transport=”http://schemas.xmlsoap.org/soap/http” />
<wsdl:operation name=”getQuote”>
<soap:operation soapAction=”urn:StockQuote/getQuote” />
<wsdl:input>
<soap:body use=”literal” />
</wsdl:input>
<wsdl:output>
<soap:body use=”literal” />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>

<wsdl:service name=”StockQuoteService”>
<wsdl:port name=”StockQuotePort” binding=”tns:StockQuoteBinding”>
<soap:address location=”http://localhost/WebServices/SOAP_EXTENSION/StockQuote/Server1.php” />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

About the author

sonal.raut
By sonal.raut

Category