SOAP WebServices in CakePHP

Tweet (function() { var s = document.createElement(‘SCRIPT’), s1 = document.getElementsByTagName(‘SCRIPT’)[0]; s.type = ‘text/javascript’; s.async = true; s.src = ‘http://widgets.digg.com/buttons.js’; s1.parentNode.insertBefore(s, s1); })(); TweetWebservices exist for a while now. They are often used behind major application that the internet user visits and uses each day. They are not visible to the ordinary user but they are [...]

Webservices exist for a while now. They are often used behind major application that the internet user visits and uses each day. They are not visible to the ordinary user but they are a strong point in the development of new applications and in the process of creating relationships between various services offered on the internet.
CakePHP provides support via the Representational state transfer (REST) library to create such webservices. However what happends if we want to use an RPC (remote procedure call) model ?

1. Understaing how the SOAP webservices work.

Basically a basic request within SOAP would look like this:

  1. Create an XML with definitions, namespaces and values.
  2. Send the XML to the WebService Server.
  3. Get the XML Response
  4. Parser the XML response

Nothing fancy. However, building the XML with namespaces, including the WSDL and so on may become a huge nightmare if developed by one. Thanks to NuSphere and Dietrich Ayala, there has been an implementation of a system withing PHP to use SOAP webservices called nuSoap. Basically, you just need to download the library, include it in your project and start sending requests.

SOAP sends requests via the Internet application layer protocol (SMTP/HTTP or HTTPS).

SOAP stands for Simple Object Access Protocol.

2. Creating the SOAP server in CakePHP

Adding support for nuSoap in CakePHP is preety easy. Just download the nuSoap library and import it into your controller as a vendor.

Example:

App::import('Vendor','nusoap');

You will need to create a custom controller special for this : Example: http://yourdomain.com/webservices/ where webservices is your controller.

To instantiate the SOAP server, you just need to run

$server = new soap_server;

Now, your Webservices controller will need to handle some functions. Basically this means that, you create the functions then register them in the soap server. Example:

$server->register(helloWorld')

function helloWorld($params) { //code here }

// 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 that is it!

3. Creating the SOAP client in CakePHP

Regarding the client: you would have the following code in your controller / action :

  1. import the nusoap library
  2. $client = new nusoap_client('http://yourdomain.com/webservices');
  3. $result = $client->call('helloWorld', array('params' => array('name' => 'Andrew' ) ) );
  4. print_r($result);

And that is it with the client – server relationship within CakePHP and Nusoap.

4. Final

Now go and make some requests!