RSS

Web Services Implementation using PHP and SOAP

29 Sep
Introduction to Web Services
Web Services is a system which enables and supports interoperability. It enables applications written in different languages, located in different places, running on different operating systems, to talk to each other, exchange information and perform different operations. The building blocks of Web Services are a set of XML based standards namely SOAP (Simple Object Access Protocol), WSDL (Web Services Description Language) and UDDI (Universal Description, Discovery, and Integration).
The above diagram is the simplest diagram which can help you visualize web services, and shows how two remote applications can interact with each other using web services. The protocol used for this remote interaction is SOAP.
Simple Object Access Protocol (SOAP)
SOAP is a lightweight protocol for the exchange of information in a distributed environment. It is an XML-based protocol. The most important aspect of SOAP is its simplicity and extensibility. A SOAP message contains data in the form of XML. SOAP consists of three parts:

  1. It is a framework for describing what is in a message and how to process the message
  2. It is a set of Encoding rules
  3. It is a convention for representing remote procedure calls (request and response)
PHP and web Services
PHP is a widely used general purpose scripting language. It is basically a procedural language, but it has the capabilities of an object oriented language also. Its syntax is similar to Perl, C, Java, etc. It can be very easily embedded in the HTML which makes web developement using PHP very easy. Some of the features of PHP which enhanced its capability for Web services development are discussed below.
Object Oriented Programming Capabilities
  • PHP supports XML
  • The CURL Extension of PHP (Client URL Library) allows us to communicate via different protocols such as HTTP, HTTPS, FTP, Telnet, LDAP).
  • The most important feature of PHP is it enables the open source web services implementation.
PHP Web Services using NuSoap
NuSoap is a group of PHP classes that allow us to send and receive SOAP messages over HTTP. You can download this nusoap.php library from http://www.nusphere.com. It is open source, licensed under the GNU LGPL. Once you have downloaded nusoap.php, you simply need to place it anywhere you like on your machine, but don’t forget to include the path of this file while including this file in your php code. I will suggest that you to keep it in the same work folder in which your code is located. To include NuSoap classes in our code:
include ("nusoap.php");

If the path of the nusoap.php file (which can be absolute or relative) is incorrect, the above-mentioned code will generate a warning but will continue the processing of the rest of the code. We can also use below-mentioned function as required.

  • require() :-it is similar to the include function but it will make the program halt in case of failure.
  • require_once() :- it is similar to require(), it won’t include the file if it has already been included.
  • include_once() :- similar to include(), it wont include if the file is already included.
Simple PHP SOAP Client Example

Below is the sample code for the client which sends the request to the remote server for validating the string, which is passed as a parameter to the server.

<?PHP
 require('nusoap.php');

 $str = 'Hello World';
 $strAr = array($str);

 $s = new soapclient('http://localhost/work_dir/validateStr.php');

 $result = $s->call('validateStr', $strAr);

 if(!$error = $s->getError())
 {
   echo 'String:' . $result;
 }
 else
 {
   echo 'Error:' . $error;
 }
?>

Code walk through

include ("nusoap.php");

Now we will see how the above code is processed. The above code shows how the client sends data to the server to validate it if it’s a valid string.

$string =’Hello World’;

This includes the NuSoap classes to the code:

$stringArray = array($string);

Create a variable for the string which you want to send to server. The parameter must be passed as an array to the SOAP client.

$s= new
soapclient(‘http://localhost/working_directory/validateString.php’);

The above code instantiate the SOAP client. Local host is used since the client and server code both are present on the same machine. If the server code is on a remote machine then the correct path of the validateString.php needs to be provided in the soap client.

$result =$s->call (‘validateString’,$stringArray);

This call method tells the soap client which service we want to access and passes the string array to the server and the method. The resulting variable stores the response from the server.

if(!$error = $s->getError()) {
echo ‘String : ‘ .$result;
}
else {
echo ‘Error:’ .$error;
}

We will be checking the error using getErrror() method. If an error occurs this method returns the string description of the error, otherwise it returns false. In the above code we are printing our result in case of success, otherwise we print the error detail.

<? php
require(‘nusoap.php);
$s= new soap_server;
$s->register(‘validateString’);
function validateString($stringValue) {
if( is_string($stringValue) {
return $stringValue;
else {
return new soap_fault(‘Client’,’’.’This is not a valid string.’);
}
}
$s->service($HTTP_RAW_POST_DATA);
?>

Simple PHP SOAP Server Example

$s= new soap_server;

Code walk through
First we instantiate the soap server using the soap_server class.

$s->register(‘validateString’);

To allow our function to be called from remote location, we need to register it with server object.

function validateString($stringValue) {
if( is_string($stringValue) {
return $stringValue;
else {
return new soap_fault(‘Client’,’’.’This is not a valid string.’);
}
}

Next we have a function definition which checks if the parameter passed to the function is a validate string or not.

$s->service($HTTP_RAW_POST_DATA);

If the input string is a valid string then it will return the string, otherwise it will return the error using soap_fault class. Finally the above method passes the incoming data from the remote client to the SOAP server’s service method. This service method processes the incoming request and calls the appropriate method.
Summary
Using PHP and NuSoap, we can develop robust and interoperable Web Services. In the above example we have seen how PHP consumes and deploys web services with SOAP. In the current scenario we have used local host but actually we can place our client and server code on different machines in the network and check the code in a distributed system.

referrence website: http://www.phpbuilder.com/columns/rajeev_rakesh_20071024.php3

Advertisements
 
1 Comment

Posted by on September 29, 2009 in PHP

 

One response to “Web Services Implementation using PHP and SOAP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: