HomePHP Page 4 - Developing SOAP Clients using PHP
PHP's SOAP Extension - PHP
SOAP (Simple Object Access Protocol) provides a flexible communication layer between applications, regardless of platform and location. As long as both the server and the client speak SOAP, they can communicate. A PHP-based web application can ask a Java database application to get some information. In this article we will try to focus on different methods of developing SOAP web service clients in PHP.
From PHP version 5.0.4, PHP has a SOAP extension. Using this SOAP Extension supports SOAP client applications with a class called SoapClient, which offers the following main functions:
SoapClient->__construct() - Constructs a new SoapClient object.
SoapClient->__soapCall() - Calls a SOAP function.
SoapClient->__getFunctions() - Returns a list of SOAP functions.
SoapClient->__getLastRequestHeaders() - Returns the last SOAP request headers.
SoapClient->__getLastRequest() - Returns the last SOAP request.
SoapClient->__getLastResponseHeaders() - Returns the last SOAP response headers.
SoapClient->__getLastResponse() - Returns the last SOAP response.
The SoapClient->__construct() constructor creates SoapClient objects in WSDL or non-WSDL mode. This function also has some options like the location and uri options, where location is the URL to request and uri is the target namespace of the SOAP service.
SoapClient->__getFunctions() allows you to get a list of functions supported by the target node. This function is only valid in WSDL mode. After getting the supported functions, SoapClient->__soapCall() allows you to make an RPC function call on the target SOAP node. Our above client implementation can also be developed using the PHP SOAP extension as shown below.
<?php //Create the SoapClient object $client = new SoapClient("http://localhost/add.wsdl");
//Call the add function of SOAP server echo $client->__soapCall("add", array(5, 7));
?>
If we are using WSDL then we can simply call the remote function as local function. The above example will generate same output as below.
<?php //Create the SoapClient object $client = new SoapClient("http://localhost/add.wsdl");
//Call the add function of SOAP server (possible only in WSDL Mode) echo $client->add(5, 7);
?>
The PHP SOAP extension is very useful for creating SOAP headers with security credentials. Usernames and passwords can be securely encrypted and then added into SOAP headers using the SOAP extension of PHP.
In this article I tried to focus on some different implementations of SOAP using PHP. Both PEAR::SOAP and NuSOAP are easy to install and use. But I personally like PHP5's SOAP extension, because it has more features than the other two. But if you are using a PHP version earlier than 5.0.4, you have to use these libraries.