Nov
27
2008

PHP5 Soap: Creating Server

As SoapClient makes it easy to interprate Web Service, SoapServer will make it easy to provide Soap Services. To create a SOAP server you simply start with a class that contains methods (functions) that you wish to allow other computers to use.

Creating a Small SOAP Server

class MySoapServer{
	public function getMessage(){
		return ’Hello, World!’;
	}
	public function addNumbers($num1, $num2){
		return $num1 + $num2;
	}
}

The above is a class, this contains the methods that we want to let people use, so we are turning our server into a Hello World Calculator.

SOAP by default does not output in WDSL mode, this is tedious and you have to create your WSDL file manually. Its recommended to use WSDL as it gives your users the methods and data types available. The server prefers WSDL as it makes handling complex data simpler.

In this example we will operate in non-WSDL mode.

In the example bellow we inform SOAP that the class we went it to work with is MySoapServer (shown above):

$options = array(’uri’ => ’http://example.org/soap/server/’); //Define Soap Options
$server = new SoapServer(NULL, $options); //Start soapserver and provide it the options
$server->setClass(’MySoapServer’);//Tell Soap which Class to work with
$server->handle(); //process incoming requests

Useing our new SOAP Server

The followng code shows how a computer can interact with our SOAP server, you will notice how the client is able to access getMessage() and addNumbers():

//Define Options
$options = array(
’location’ => ’http://example.org/soap/server/server.php’,
’uri’ => ’http://example.org/soap/server/’
);
$client = new SoapClient(NULL, $options); //Create SOAP Class and give it Options
echo $client->getMessage() . "\n"; // This will output Hello WOrld
echo $client->addNumbers(3, 5) . "\n"; //This will output 8
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • LinkedIn
  • Live
  • StumbleUpon
  • Technorati
  • TwitThis
Written by Adam in: 06. XML and Web Services |

No Comments »

RSS feed for comments on this post. TrackBack URL

Leave a comment

WordPress Powered, Theme by TheBuckmaker.com | Add to Technorati Favorites. | RSS and Comments RSS