Menu:

Sponsor

Discover Master of Alchemy, our first iPad/iPhone and iPod touch game!

Follow Me

 

Forum's topics

Latest Files

Archives

Top Rated

Categories

Photo Gallery


Create a WebService with PEAR::SOAP

require_once('SOAP/Server.php');
require_once('SOAP/Disco.php');

First you need to include the SOAP package from PEAR. Without these files you can't do anything because they will convert all our code into a working WebService.

From the PathFinder class we just have to know that:

PathFinder operations

1) to be initialized from a WebService client we have to call function initialize($map). Where $map is an Array of strings.
For example:

$ar = array();
$ar[] = array("1,0,0,1,0,0,1");
$ar[] = array("1,0,0,0,1,1,1");

then PathFinder::initialize($ar);

That kind of array means simply that the "0" are empty positions and "1" are not allowed positions (a wall for example in your map representation)
We need to declare to our SOAP service the type of the accepted arguments (in this case the $map argument). In fact inside the PathFinder constructor:

function PathFinder()
{
	// define operation name (initialize)
	// its parameters (in)
	// the return object (out)
	$this->__dispatch_map['initialize'] = array(
		"in"  => array("map"  => "{urn:PathFinder}ArrayOfStrings"),
		"out" => array("code" => "string")
	);
	$this->__dispatch_map['findPath'] = array(
		"in"  => array("sid"  => "string", "startPosition" => "string", "endPosition" => "string"),
		"out" => array("code" => "{urn:PathFinder}ArrayOfStrings")
	);
	$this->__typedef['ArrayOfStrings'] = array(array('item'=>'string'));
}

For every public methods (all the function accessible from outside) I delared both "in" argument type and "out" result type. This is needed also for the webservices clients in order to know which type of argument to be passed and results expected.
For example, for the "initialize" function I have delcared as "in" param an ArrayOfStrings, which is not a predefined soap val (predefined are strings, int...), so i need also to define this argument type.
And this is made in this line:

$this->__typedef['ArrayOfStrings'] = array(array('item'=>'string')); 

This means that my php class will expect as argument an array of items, which items are strings at least.
then I made the same operation for all the other methods of my WebService.

Once initialized, with initialize(), the class will store the $map array into a SESSION and will return the session ID to the client. Then the client will pass, everytime it will need to call the findPath method, this session id, to let the php class retrieve the $map value stored for that client.
As you can see in the last lines of the initialize method:

$_SESSION['map'] = $ar;
return session_id();

It's important to save the $map array passed from the client, because the client itself will not invoke the two methods (initialize and findPath) in the same single call, but in different moments, and moreover the initialize will be called just once (the first time) and then will invoke the findPath method more than once. For this reason PHP needs to store the array information for that particular client into a SESSION in order to get the map of that client once it will invoke the service later (passing the session_id back).

In order to see practically what we've just done... In Dreamweaver I define the path to my webservice php file (see picture)

And then DW will analyze the WSDL code and return a tree representation of my WS (next picture):

As you can see DW has found the two operation i have defined (initialize and findPath). It recognize also that for initialize the result will be a string, for findPath is an array of strings (String[]), and the input param must be an aray of strings (String[]).
For the findPath method, instead, the required arguments are: a string "endPosition" (which is something like "12,5"), a string "startPosition" and a string "sid" (which is the result of the initizlize method).

Start Serve

Once defined all the methods of our class we need to initialize the SOAP and tell it to use our class for the Webservice operations.

Start the SOAP Server with this line:
$server = new SOAP_Server();

Now initialize the PathFinder class:
$webservice = new PathFinder();

And finally set the Pathfinder initialized object ($webservice) into the $server object (that is: set the webservice object as the default responder for the WSDL operations) :

// set the path finder class as default responder for the WSDL class<
$server->addObjectMap($webservice,'http://schemas.xmlsoap.org/soap/envelope/');