Infolink

 

Search This Blog

Aug 15, 2012

Introduction to Web Services

Web Services can convert your application into a Web-application, which can publish its function or message to the rest of the world.

Web Services are published, found, and used through the Web.

What are Web Services?

 

Web services are application components
Web services communicate using open protocols
Web services can be used by other applications
XML is the basis for Web services

How Does it Work?

 

The basic Web services platform is XML + HTTP.
XML provides a language which can be used between different platforms and programming languages and still express complex messages and functions.
The HTTP protocol is the most used Internet protocol.
Web services platform elements:
SOAP (Simple Object Access Protocol)
UDDI (Universal Description, Discovery and Integration)
WSDL (Web Services Description Language)
Why Web Services?
1.Interoperability has Highest Priority
When all major platforms could access the Web using Web browsers, different platforms could interact. For these platforms to work together, Web-applications were developed.
Web-applications are simple applications that run on the web. These are built around the Web browser standards and can be used by any browser on any platform.
Web Services take Web-applications to the Next Level
By using Web services, your application can publish its function or message to the rest of the world.
Web services use XML to code and to decode data, and SOAP to transport it (using open protocols).
With Web services, your accounting department's Win server's billing system can connect with your IT supplier's UNIX server.
Web Services have Two Types of Uses
Reusable application-components.
There are things applications need very often. So why make these over and over again?
Web services can offer application-components like: currency conversion, weather reports, or even language translation as services.
Connect existing software.
Web services can help to solve the interoperability problem by giving different applications a way to link their data.
With Web services you can exchange data between different applications and different platforms
Web Services Platform Elements
Web Services have three basic platform elements:
SOAP, WSDL and UDDI.

What is SOAP?

 

SOAP is an XML-based protocol to let applications exchange information over HTTP.
Or more simple:
SOAP is a protocol for accessing a Web Service.
SOAP stands for Simple Object Access Protocol
SOAP is a communication protocol
SOAP is a format for sending messages
SOAP is designed to communicate via Internet
SOAP is platform independent
SOAP is language independent
SOAP is based on XML
SOAP is simple and extensible
SOAP is a W3C standard
Syntax Rules
Here are some important syntax rules:
A SOAP message MUST be encoded using XML
A SOAP message MUST use the SOAP Envelope namespace
A SOAP message MUST use the SOAP Encoding namespace
A SOAP message must NOT contain XML Processing Instructions
Skeleton SOAP Message
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encoding>
<soap:Header>
...
</soap:Header>
<soap:Body>
...
<soap:Fault>
...
</soap:Fault>
</soap:Body>
</soap:Envelope>
he SOAP Envelope element is the root element of a SOAP message.
The SOAP Envelope Element
The required SOAP Envelope element is the root element of a SOAP message. This element defines the XML document as a SOAP message.
SOAP Header Element
The SOAP Header element contains header information.
SOAP Body Element
The SOAP Body element contains the actual SOAP message.
The SOAP Body Element
The required SOAP Body element contains the actual SOAP message intended for the ultimate endpoint of the message.
SOAP Fault Element
The SOAP Fault element hold errors and status information for a SOAP message.
The SOAP Fault Element
The optional SOAP Fault element is used to indicate error messages.
If a Fault element is present, it must appear as a child element of the Body element. A Fault element can only appear once in a SOAP message.

What is WSDL?

WSDL is an XML-based language for locating and describing Web services. how to access them
WSDL stands for Web Services Description Language
WSDL is based on XML
WSDL is used to describe Web services
WSDL is used to locate Web services
WSDL is a W3C standard
WSDL Documents
A WSDL document is just a simple XML document.
It contains set of definitions to describe a web service.
The WSDL Document Structure
A WSDL document describes a web service using these major elements:
Element Defines
<types> The data types used by the web service
<message> The messages used by the web service
<portType> The operations performed by the web service
<binding> The communication protocols used by the web service
The main structure of a WSDL document looks like this:<definitions>
<types>
definition of types........
</types>
<message>
definition of a message....
</message>
<portType>
definition of a port.......
</portType>
<binding>
definition of a binding....
</binding>

The <portType> element is the most important WSDL element.
It describes a web service, the operations that can be performed, and the messages that are involved.
The <portType> element can be compared to a function library (or a module, or a class) in a traditional programming language.

WSDL Messages

The <message> element defines the data elements of an operation.
Each message can consist of one or more parts. The parts can be compared to the parameters of a function call in a traditional programming language.

WSDL Types

The <types> element defines the data types that are used by the web service.
For maximum platform neutrality, WSDL uses XML Schema syntax to define data types.
WSDL Bindings
The <binding> element defines the message format and protocol details for each port.

What is UDDI?

UDDI is a directory service where search for Web services.
UDDI stands for Universal Description, Discovery and Integration
UDDI is a directory for storing information about web services
UDDI communicates via SOAP

SOAP Soup

Simple Object Access Protocol is one of the neatest XML based technologies, the SOAP soup we have in front of us and create a foundation for truly understanding what SOAP is all about
Simple Object Access Protocol (SOAP)
As the root of our conversation, SOAP has been developed to solve an aging problem with developing applications for the Internet: interoperability. Imagine a world where you can access objects and services on remote (or local) servers in a platform-independent manner. Today’s world is polluted with different operating systems, different firewalls, different methods of making remote procedure calls, and different platforms. In order to interoperate across the Internet both the client and server need to understand each others security types and trusts, service deployment, and implementation details, not to mention speak the same platform language

With SOAP, an end to all of this platform-specific confusion has arrived. Based on the already industry wide accepted HTTP standard and W3C XML standard, SOAP bridges the gap between competing object and provides a light-weight messaging format that works with any operating system, any programming language, and any platform.

Simply stated, SOAP provides a way to access services, objects, and servers in a completely platform-independent manner. Using SOAP you can query, invoke, communicate with, and otherwise touch services provided on remote systems without regard to the remote system’s location, operating system, or platform.
Service Description Language (SDL)

In order to utilize a service on a server using SOAP, you (your program) need to understand what the remote service is capable of. Notice the difference between understanding the capabilities of the remote service and understanding the remote service’s implementation (platform specific information). Using SDL, SOAP provides a way to query the remote service and learn about its capabilities, such as how it represents data types and commands. For example, using SDL one could learn that a particular service accepts two integers, adds them, and returns the sum as an integer. 

SDL is an XML grammar, or in other words, SDL is simply an XML document, thus it inherently has all of the great XML traits (e.g. it is extensible). and SDL is platform independent (another trait of XML).

SOAP Contract Language (SCL)

Much like SDL, the SOAP Contract Language is designed to expose the capabilities of a service, but also goes much farther. Microsoft has published the SCL specification with the following comment, “We published the SCL specification so we could solicit broad feedback from the industry. We’ll update the public toolkit to use SCL once the specification takes into consideration the feedback we’re going to get and is in a reasonably stable state. Until then it (the toolkit) will continue to use a form of SDL.” Because of the extended capabilities of SCL, it will replace SDL once it matures.
In addition to describing a service’s capabilities, SCL goes much further by defining the actual “contract” by which endpoints (the server and the client) communicate. This contract contains both a description of the endpoint and the messages it will accept, information that describes the flow of messages between endpoints. 

Discovery of Web Services (DISCO)

DISCO provides a way to discover and retrieve an SDL or SCL description of services on remote machines. This is the final piece of SOAP that puts everything together. Using the Discovery Document Format (which is also an XML grammar), one can send a discovery document to a remote server and, if any SOAP enabled services (AKA web service) exist, receive back an SDL or SCL description of the services provided.
Remote Object Proxy Engine (ROPE)
ROPE provides a set of COM components that assist in building SOAP messaging into your applications. The ROPE components (in the form of rope.dll) have two models of complexity, one for VB-like simplicity and one for VC-like control.
If you were to call a SOAP method without using ROPE you would have to format the SOAP calls into XML, package the call into an HTTP request and send it, receive the HTTP response and parse the returning XML. ROPE takes care of all of that complexity for you. ROPE is not required to use SOAP, but it sure makes things easier.
SOAP Toolkit

Microsoft has made it easy to begin working with SOAP by publishing a SOAP toolkit for Visual Studio 6.0. While the toolkit is not supported, it does contain a healthy help file to help you learn how to use it. Using the toolkit you do not need to master XML and SOAP or learn wire protocol. It makes adding SOAP capabilities to your applications very easy and your knowledge and experience in existing tools (Visual Studio).

Summary

As you can see, SOAP is a complete technology ready to be put to use. Each part of SOAP has been well thought out and developed, and will make developing Internet applications and services much easier.

SOAP with PHP

SOAP, the Simple Object Access Protocol, is the powerhouse of web services. It’s a highly adaptable, object-oriented protocol that exists in over 80 implementations on every popular platform, including JavaScript, It provides a flexible communication layer between applications, regardless of platform and location. As long as they both speak SOAP, a PHP-based web application can ask a C++ database application on another continent to look up the price of a book and have the answer right away.
SOAP was created collaboratively as an open protocol. Early in its development, soap encodes messages as XML, and use HTTP to transport those messages. SOAP, however, can use other transport protocols, offers a number of high-end features, and is developing rapidly.

A SOAP transaction begins with an application making a call to a remote procedure. The SOAP client script then encodes the procedure request as an XML payload and sends it over the transport protocol to a server script. The server parses the request and passes it to a local method, which returns a response. The response is encoded as XML by the server and returned as a response to the client, which parses the response and passes the result to the original function.

There are a number of different implementations of SOAP under PHP. It’s a shifting landscape: new ones appear, and old ones aren’t maintained or simply vanish. As of this writing, the most viable PHP implementation of SOAP seems to be Dietrich Ayala’s SOAPx4, also known as NuSOAP. This implementation is the most commonly used and appears to be the most fully developed and actively maintained, and it shows every sign of continuing to be a robust and popular solution. It’s not complete—a number of features, including full documentation, are still in the works—but it’s still a highly viable and easy-to-use SOAP solution.

Introduction to NuSOAP


NuSOAP is a group of PHP classes that allow developers to create and consume SOAP web services. It does not require any special PHP extensions.
Hello, World
Showing no imagination whatsoever, I will start with the ubiquitous "Hello, World" example. This will demonstrate the basic coding of NuSOAP clients and servers.
I will start with the server code, since without a server there is no need to have any client. The server exposes a single SOAP method named Hello, which takes a single string parameter for input and returns a string. Hopefully, the comments within the code provide sufficient explanation! For the simplest services, all that will change are the functions being defined and registered.

<?php
// Pull in the NuSOAP code
require_once('nusoap.php');
// Create the server instance
$server = new soap_server;
// Register the method to expose
$server->register('hello');
// Define the method as a PHP function
function hello($name) {
return 'Hello, ' . $name;
}

// Use the request to (try to) invoke the service
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>

The client for this service is below. There are a few important things to note. First, when the instance of soapclient is created, the parameter specified is the URL to the service. In my case, helloword.php is accessed from http://localhost/phphack. The services you consume will, of course, be located at different URLs. Second, when calling the service, the first parameter is the service name. This must match the case-sensitive name of a method available on the server. In this example, it must match the method registered within helloworld.php. Finally, the second parameter in the call is an array of parameters that will be passed to the SOAP service method. Since the hello method of helloworld.php requires a single parameter, this array has one element.

<?php
// Pull in the NuSOAP code
require_once('nusoap.php');
// Create the client instance
$client = new soapclient('http://localhost/phphack/helloworld.php');
// Call the SOAP method
$result = $client->call('hello', array('name' => 'Scott'));
// Display the result
print_r($result);
?>

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...