Sjoerd Maessen blog

PHP and webdevelopment

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

with 2 comments


Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/public_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/public_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/public_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/public_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/public_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/public_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/public_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/public_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/public_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/public_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/public_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/public_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/public_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/public_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/public_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/public_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/public_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/public_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/public_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/public_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/public_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/public_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/public_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/public_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/public_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/public_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/public_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/public_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/public_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/public_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

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. span class=”coMULTI”>/**
  2.  * Blog service class
  3.  *//**
  4.          * Return a blogpost
  5.          *
  6.          * @param string $sID
  7.          * @return string test data
  8.          */// Notice the missing id number 4
  9.  
  10.                  // Check if we requested a valid blogpost id
  11. ‘Server’, ‘Blogpost with id ‘.$iID.‘ not found!’‘Blogpost with id ‘

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. ‘blog.wsdl’);
  4.  
  5. // Register the blog service class and all the methods
  6. ‘BlogService’);
  7.  
  8. // Rock ‘n roll

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. span class=”st0″>"soap.wsdl_cache_enabled", "0"); // Disable the wsdl cache
  2. // Url to our wsdl, http://{siteUrl}/webservice/index.php?wsdl is also possible
  3.                                 ‘{siteUrl}/webservice/blog.wsdl’‘trace’‘exceptions’

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

2 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

Leave a Reply