Sjoerd Maessen blog

PHP and webdevelopment

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

with 6 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

Written by Sjoerd Maessen

February 11th, 2010 at 1:26 pm

Posted in SOAP

Tagged with

6 Responses to 'Introduction into SOAP, setting up a simple webservice with PHP SOAP'

Subscribe to comments with RSS or TrackBack to 'Introduction into SOAP, setting up a simple webservice with PHP SOAP'.

  1. Thanks, really helpful! I was struggling for 3 days to get it right. Now I’ve got the correct setup!

    Ben

    27 Oct 11 at 2:27 pm

  2. Hi,

    Thanks to share this information. How can we create different class for different functionality of web service?

    jayendra

    11 Nov 11 at 2:14 pm

  3. Le 06/02/2012 à 16h20 J’adore Imogen Poots, elle est sublime! J’aime beaucoup l’ambiance de cette pub/clipEt la collection a l’air chouette, y a pas de robes et les imprimés sont joli, moi je dis oui

    http://www./

    13 Nov 16 at 3:52 pm

  4. Thhis design is steller! You ɗefinitely ҝnow how to keeρ a reader amused.
    ᗷetween your wit and your videos, І was almost moved tо start my oᴡn blog (wеll, ɑlmost…HaHa!) Excellent
    job. I гeally loved ѡhat үou had to sаy, and more than tһat,how you pesented it.
    Tooo cool!

  5. I’m glad to have worked with you as your publishing consultant!By simply sharing your story, you were able to successfully shed light to this ongoing concern.More power to you and your book!

  6. je ne comprends pas que vous ne compreniez pas en quoi la requalification de la France dans la mondialisation, dans l’économie mondiale, au travers de ses atouts spécifiques, ce que JCF appelle les « avantages différenciateurs » puisse être de nature à rendre la France plus compétitive. Et je crois percevoir dans votre propos le fait que la compétitivité de la France, ça ne profiterait qu’aux riches. Je dois me tromper.

Leave a Reply