Sjoerd Maessen blog

PHP and webdevelopment

Introduction into SOAP, setting up a simple webservice with PHP SOAP

without comments

I was asked to create a simple webservice that would allow us to transfer a intranet post to an external CMS. In this post I will explain the steps you must take to set-up a simple webservice with the PHP SOAP extension.

The first step, create a simple class that we will use to request data from
We will create a class with one method that returns a string with the parameter we called it. The method will accept one parameter and will check if the value is correct.

  1.  
  2. <?php
  3. /**
  4.  * Blog service class
  5.  */
  6. class BlogService
  7.  {
  8.         /**
  9.          * Return a blogpost
  10.          *
  11.          * @param string $sID
  12.          * @return string test data
  13.          */
  14.          public function getItem($sID)
  15.          {
  16.                  $iID           = (int)$sID;
  17.                  $aValidIds = array(1, 2, 3, 5); // Notice the missing id number 4
  18.  
  19.                  // Check if we requested a valid blogpost id
  20.                  if(!in_array($iID, $aValidIds)) {
  21.                          return new SoapFault(‘Server’, ‘Blogpost with id ‘.$iID.‘ not found!’);
  22.                  }
  23.                  
  24.                  return ‘Blogpost with id ‘.$iID;
  25.          }
  26.  }
  27. ?>
  28.  

Nothing special here, notice that we don’t throw a normal exception but return the SoapFault instead so we can handle the error clientside.

The second step, create a WSDL document

The WSDL document is a simple XML document that describes:
- the service itself
- the operations of the service
- the data types used in the service

The WSDL will describe our method getItem from our Blog service class so we can call it later on.

So how does this WSDL thing look like?

  1.  
  2.  // Init the server
  3. $oServer = new SoapServer(‘blog.wsdl’);
  4.  
  5. // Register the blog service class and all the methods
  6. $oServer->setClass(‘BlogService’);
  7.  
  8. // Rock ‘n roll
  9. $oServer->handle();
  10.  

The fourth and final step, test our webservice

Its a good idea to test the webservice with a program like soapUI, it often can provide some additional information when troubleshooting. (They have a free version available on their website).

To test our webservice in PHP we can use the following code:

  1.  
  2. ini_set("soap.wsdl_cache_enabled", "0"); // Disable the wsdl cache
  3.  
  4. $oClient = new SoapClient(
  5.                                 // Url to our wsdl, http://{siteUrl}/webservice/index.php?wsdl is also possible
  6.                                 ‘{siteUrl}/webservice/blog.wsdl’,
  7.                                 array(
  8.                                         ‘trace’  => true,
  9.                                         ‘exceptions’=> true
  10.                                 ));
  11.  
  12. try {
  13.         $aResult = $oClient->getItem(4);
  14.         var_dump($aResult);
  15. } catch (SoapFault $e) {
  16.         exit($e->faultstring);
  17. }
  18.  

You should see a SOAP fault, because the id number 4 we gave to the getItem function didn’t exist in our Blog service class. Change the value to 1,2,3 or 5 and you should get a nice response back.
Another pretty neat function is $oClient->__getFunctions(); it will return all the function that are available in the webservice.

This was just a short introduction to SOAP, a lot more is possible with SOAP.
Special thanks to David Zuelke for the nice SOAP introduction at the PHPbenelux conference 2010.

Example files:
Webservice.rar

Just deploy the example files on your webserver, change the {siteUrl} values in the files with your domain and call the client.php

  • Share/Bookmark

Written by Sjoerd Maessen

February 11th, 2010 at 1:26 pm

Posted in SOAP

Tagged with

Leave a Reply