Wednesday, March 24, 2010

WebService ::: Part3:Sample webservice program in PHP (nusoap)



Sample Webservice :Addition of two number

Server.php



// load SOAP library
require_once('D:\wamp\www\nusoap-0.7.3\lib\nusoap.php');
//require_once('../lib/nusoap.php');
//require_once('D:/wamp/www/nusoap-0.7.3/samples/functions.php');
// load library that holds implementations of functions we're making available to the web service
// set namespace
$ns="http://localhost/";
// create SOAP server object
$server = new soap_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)
{
$total=$int1+$int2;
return $total;
}
// service the methods
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA)? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);

?>

Client.php


require_once('D:/wamp/www/nusoap-0.7.3/lib/nusoap.php');
//require_once('../lib/nusoap.php');
$wsdl="http://localhost/nusoap-0.7.3/samples/server.php?wsdl";
$client=new soapclient($wsdl, 'wsdl');
$param=array('int1'=>'15', 'int2'=>'340');
$result=$client->call('ws_add', $param);
//echo "";
// Check for a fault
if ($client->fault)
{
echo 'Fault';
print_r($result);

}
else
{
// Check for errors
$err = $client->getError();
if ($err)
{
// Display the error
echo 'Error' . $err . '';
}
else
{
// Display the result
echo 'Result';
print_r($result);
echo '';
}
}
// Display the request and response
echo 'Request';
echo '' . htmlspecialchars($client->request, ENT_QUOTES) . '';
echo 'Response';
echo '' . htmlspecialchars($client->response, ENT_QUOTES) . '';
unset($client);



?>

Tuesday, March 23, 2010

WebService ::: Part2:-introduction



What is Web Service?
Web Services are applications that can be published, located, and invoked across the Internet. A Web service is a unit of managed code that can be remotely invoked using HTTP, that is, it can be activated using HTTP requests.
The World Wide Web Consortium (W3C) has defined the web services. According to W3C, “Web Services are the message-based design frequently found on the Web and in enterprise software. The Web of Services is based on technologies such as HTTP, XML, SOAP, WSDL, SPARQL, and others.”

Web Services Architecture




  • Service Processes: This part of the architecture generally involves more than one Web service. For example, discovery belongs in this part of the architecture, since it allows us to locate one particular service from among a collection of Web services.
  • Service Description: One of the most interesting features of Web Services is that they are self-describing. This means that, once you've located a Web Service, you can ask it to 'describe itself' and tell you what operations it supports and how to invoke it. This is handled by the Web Services Description Language (WSDL).
  • Service Invocation: Invoking a Web Service involves passing messages between the client and the server. SOAP (Simple Object Access Protocol) specifies how we should format requests to the server, and how the server should format its responses.
  • Transport: Finally, all these messages must be transmitted somehow between the server and the client. The protocol of choice for this part of the architecture is HTTP (Hyper Text Transfer Protocol), the same protocol used to access conventional web pages on the Internet.
  • Universal Description, Discovery, and Integration (UDDI) is a protocol for describing available Web services components. This standard allows businesses to register with an Internet directory that will help them advertise their services, so companies can find one another and conduct transactions over the Web. This registration and lookup task is done using XML and HTTP(S)-based mechanisms.

Why Web Services?

Here are the benefits of using Web Services:

  1. Exposing the function on to network: A Web service is a unit of managed code that can be remotely invoked using HTTP that is, it can be activated using HTTP requests. So, Web Services allows you to expose the functionality of your existing code over the network. Once it is exposed on the network, other application can use the functionality of your program.
  2. Connecting Different Applications: Web Services allows different applications to talk to each other and share data and services among themselves. So, Web services is used to make the application platform and technology independent.
  3. Standardized Protocol: Web Services uses standardized industry standard protocol for the communication. All the four layers (Service Transport, XML Messaging, Service Description and Service Discovery layers) uses the well defined protocol in the Web Services protocol stack. This standardization of protocol stack gives the business many advantages like wide range of choices, reduction in the cost due to competition and increase in the quality.
  4. Low Cost of communication: Web Services uses SOAP over HTTP protocol for the communication, so you can use your existing low cost internet for implementing Web Services. This solution is much less costly compared to proprietary solutions like EDI/B2B.
  5. Support for Other communication means: Beside SOAP over HTTP, Web Services can also be implemented on other reliable transport mechanisms. So, it gives flexibility use the communication means of your requirement and choice. For example Web Services can also be implemented using ftp protocol (Web services over FTP).
  6. Loosely Coupled Applications: Web Services are self-describing software modules which encapsulates discrete functionality. Web Services are accessible via standard Internet communication protocols like XML and SOAP. These Web Services can be developed in any technologies (like c++, Java, .NET, PHP, Perl etc.) and any application or Web Services can access these services. So, the Web Services are loosely coupled application and can be used by applications developed in any technologies.
  7. Web Services Sharing: These days due to complexness of the business, organizations are using different technologies for distributing computing. Web Services supports all these technologies, thus helping the business to use existing investments in other technologies.
  8. Web Services are Self Describing: Web Services are self describing applications, which reduces the software development time. This helps the other business partners to quickly develop application and start doing business. This helps business to save time and money by cutting development time.
  9. Automatic Discovery: Web Services automatic discovery mechanism helps the business to easy find the Service Providers. This also helps your customer to find your services easily. With the help of Web Services your business can also increase revenue by exposing their own Web Services available to others.
  10. Business Opportunity: Web Services has opened the door to new business opportunities by making it easy to connect with partners.

Webservice ::: Part1:-SOA introduction

Service Oriented Architecture or SOA for short is a new architecture for the development of loosely coupled distributed applications. In fact service-oriented architecture is collection of many services in the network. These services communicate with each other and the communications involves data exchange & even service coordination
Nowadays SOA is based on the Web Services.
Broadly SOA can be classified into two terms: Services and Connections.
Services:
A service is a function or some processing logic or business processing that is well-defined, self-contained, and does not depend on the context or state of other services.
Connections:
Connections means the link connecting these self-contained distributed services with each other, it enable client to Services communications. In case of Web services SOAP over HTTP is used to communicate the between services.

Benefits of the Service Oriented Architecture
  • Due to its platform independence, it allows companies to use the software and hardware of their choice.
  • There is no threat of vendor lock-in .
  • SOA enables incremental development, deployment, and maintenance.
  • Companies can use the existing software (investments) and use SOA to build applications without replacing existing applications .
  • The training costs are low, so the available labor pool can be used for running the applications
Example of the service-oriented architecture
Above figure shows how a service consumer sends a service request to a service provider. After accepting the request, service provider send a message to the service consumer. In this case a service provider can also be a service consumer.