PHP Web Services have been popping up all over the internet since past few years. There could not be any better language than PHP for building your own Web Service in simplistic manner.
PHP is a great scripting language with many advantages. It provides the power to connect to variety databases (that are available with hosting providers) and easy development curve for faster development along with high response time on account of the underlying libraries compiled for performance.
A database is a crucial part of any PHP Web Service, however it is not mandatory to have one for Web Service. It is just that without it a Web Service is not that useful. It helps you store information about user query and visit information.
What are Web Services?
In a typical scenario, a visitor visits a website and uses the functionality provided by application hosted. HTTP request is send to the server from web browser and server responses are translated back by browser to display the desired result of the visitor.
But things have changed a lot with the evolution of web technologies. You don’t need to visit a website to use its service and functionality if it is providing a Web Service.
With Web Service you can exchange data between a server and a client using a standard XML format to ‘package’ requests and data so that both systems can ‘understand’ each other properly. Either a server or a client could be a Web Server, or any other electronic device you could think of for that matter.
There are different methods for providing Web Services but the most common methods are SOAP, XML-RPC and REST.
What is NuSOAP?
NuSOAP is a group of PHP classes that allow developers to create and consume SOAP web services without needing any special PHP extensions.
It stands for Simple Object Access Protocol, and it is essentially a standard for exchanging XML based data via HTTP.
Having understood Web Services and NuSOAP let’s start building our own Web Service using PHP.
Requirements: PHP 5 and above, Apache Webserver, NuSOAP library and MySQL (required in case there are database interactions)
To understand the concept of the Web Service are going to create a ‘calculator’ Web Service and consume it too.
Create a new project ‘webservice’ on the webroot and unzip the NuSOAP file and place it in this folder.
Firstly, create a new file calc_server.php in the NuSOAP directory (for demonstration purpose I have created it in NuSOAP directory).
The PHP file will look like this:
// load SOAP library
require_once("lib/nusoap.php");
// set namespace
localhost in my case
$ns = "http://localhost/";
// create SOAP server object
$server = new nusoap_server();
// setup WSDL file, a WSDL file can contain multiple services
$server->configureWSDL('Calculator', $ns);
$server->wsdl->schemaTargetNamespace = $ns;
// register a web service method
$server->register('ws_add',
array('int1' => 'xsd:integer','int2' => 'xsd:integer'),// input parameters
array('total' => 'xsd:integer'),// output parameter
$ns, // namespace
"$ns#ws_add", // soapaction
'rpc', // style
'encoded', // use
'adds two integer values and returns the result' // documentation
);
function ws_add($int1, $int2){
return new soapval('return','xsd:integer', add($int1, $int2));
}
// implementation of add function
function add($int1, $int2) {
return $int1 + $int2;
}
//substract method
$server->register('ws_substract',
array('int1' => 'xsd:integer', 'int2' => 'xsd:integer'),// input parameters
array('total' => 'xsd:integer'),// output parameter
$ns, // namespace
"$ns#ws_substract", // soapaction
'rpc', // style
'encoded', // use
'substracts two integer values and returns the result' // documentation
);
function ws_substract($int1, $int2) {
return new soapval('return','xsd:integer',substract($int1, $int2));
}
// implementation of add function
function substract($int1, $int2) {
if($int1 > $int2)
return $int1 - $int2;
else
return $int2 - $int1;
}
//multiplication
$server->register('ws_multiply',
array('int1' => 'xsd:integer', 'int2' => 'xsd:integer'),
array('total' => 'xsd:integer'),
$ns,
"$ns#ws_multiply", // soapaction
'rpc',
'encoded',
'multiplies two integers and returns the result'
);
function ws_multiply($int1, $int2) {
return new soapval('return', 'xsd:integer', multiply($int1, $int2));
}
// implementation of multiply function
function multiply($int1, $int2) {
return $int1 * $int2;
}
// service the methods
$server->service($HTTP_RAW_POST_DATA);
Save the file. It will look like this when you browse it.
NuSOAP library has a very good feature of Web Service Inspection. If you open the calc_server.php file in a browser, you will see something like this:
With this we have now created our own ‘calculator’ Web Service. Now, let us understand how to consume this new created NuSOAP Web Service in PHP.
Create a new PHP file, say ‘calc_client.php’ and put the following code in it.
//client code
<?php
require_once('lib/nusoap.php');
//create a parameter array to pass to the webservicefunction call
$param = array('int1'=>'15.00', 'int2'=>'10');
//define the webservice
$wsdl = "http://localhost/webservice/nusoap/calc_server.php?wsdl";
//create client object
$client = new nusoap_client($wsdl, 'wsdl');
$response = $client->call('ws_add', $param);
//print the response
print_r($response);
?>
The result is the addition of 2 numbers and it will be displayed as below:
It is that simple to create and consume Web Service using NuSOAP library. Moreover, NuSOAP library can also use XML-RPC and REST methods for Web Services.
I have shown you creation and consumption of PHP Web Service using NuSOAP library. If you want to play more with it then perhaps you might even consider adding an interface to the client for INSERTing and UPDATEing new items.
No comments:
Post a Comment