The Coded One

programming, algorithms, discrete math, open-source

Introduction to SOAP

with 3 comments

What is SOAP?

By merely looking at what SOAP stands for, we will already have an idea of what it is. So lets try to break it down:

  • Simple – easy, non-complex
  • Object – Let’s use the programming cliche:  “Everything is an object”
  • Access – the right to obtain or make use of
  • Protocol – A set of rules that lets computers agree how to communicate over the net

So what does this tell us? Soap is an easy way of getting or using objects across a network or commonly, the Internet.

Advantages of Using SOAP?

  1. It is platform and language independent.  It doesn’t matter if the SOAP client was developed using PHP in an Apache/Linux environment and the SOAP server that you’re connecting to is written in Java and is running in a Windows Server.  All interaction happens by sending an XML file back and forth.
  2. Since it uses HTTP ( port 80 ) which I would assume is open for all systems that would want to use the service, there’s no need to have another port opened.  In short, it goes through the firewall by default.
  3. It promotes encapsulation.  There’s no need for you to worry how the SOAP server will implement the processing of your request and vice-versa.  Supply the correct parameters and the server will act accordingly.

What is WSDL

WSDL stands for Web Services Description Language.  You can think of it as a skeleton that tells you what parameters are expected by the server as well as the return types.   You use it as a guide when implementing an API(Application Programming Interface).

Getting Started – PHP 4

In PHP 4, there was no native function for handling SOAP calls however there is a library called NuSOAP that you can use for this purpose.

Here is how a demo SOAP server would look like:

<?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);
?>

And here is the code for the SOAP client:

<?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);
?>

If you want to learn more, a good tutorial can be found here.

Getting Started – PHP 5

With the advent of version five came lots of great additions to PHP including a native SOAP Class/Library.  A class reference/guide can be found here.

Now I will not go through each of the functions as the class reference provides everything that you need.  Basically, the library provided by PHP as well as NuSOAP rids us of going through the trouble of writing the XML (envelope, header, etc) ourselves.

Written by Mark Basmayor

February 26, 2009 at 5:33 pm

Posted in PHP

Tagged with , ,

3 Responses

Subscribe to comments with RSS.

  1. hello!

    also the advantage of using soap is that, it is one of the industry standard api which some big companies are currently using.

    thanks for following me on twitter.

    regard,

    brainv

    March 9, 2009 at 11:21 am

  2. Hi brainv, that is correct, an example of which is amazon. Thanks for your comment.

    marksman

    March 12, 2009 at 9:28 am

  3. just what i was looking for, i was wondering how to get weather forecast from NOAA, only thing left now is how to do it in codeigniter…

    thanks boz! 😀

    Boyet

    May 3, 2009 at 9:09 pm


Leave a comment