PHP-SOAP web service with out a WSDL

This article is going to describe how to create a simple web service with php-soap step by step.


    Server to run php (apache, xampp , wamp)
    PHP-SOAP extension


Let's write a simple web service to find the length of a given string.

So here is the code for StringLengthServer.php

# StringLengthServer.php
# Copyright (c) 2011 by Lakmali Baminiwatta

    function findLength($string) {
           return "Length of the string " . $string . "is : ".strlen($string);
    $server = new SoapServer(null,
              array('uri' => "urn://"));


  • First thing done here is to write a function to perform the action done by the service. So here function findLength($string) takes a string as the argument and prints back the length of it.
  • Then SoapServer is created. First argument null which means no WSDL used. Second argument is uri value is just a unique string as the namespace.
  • Then we have to add the function findLength to the server.
  • Now copy the file StringLengthServer.php to the server to run. Here it is assumed the url which service run is http://localhost/simple_server/StringLengthServer.php.
  • Now let's write a simple client program to get the service from the webservice.

    So here is the code for StringLengthClient.php
# Copyright (c) 2011 by Lakmali Baminiwatta
   $client = new SoapClient(null, array(
      'location' => "http://localhost/simple_server/StringLengthServer.php",
      'uri'      => "urn:urn://",
      'trace'    => 1 ));

   $return = $client->__soapCall("findLength",array("lakmali"));
   echo("\nReturning value of __soapCall() call: ".$return);

   echo("\nDumping request headers:\n"

   echo("\nDumping request:\n".$client->__getLastRequest());

   echo("\nDumping response headers:\n"

   echo("\nDumping response:\n".$client->__getLastResponse());

In here the location argument of the SoapClient must be the URL to server program which we wrote above and added to run. Make sure it is correct.

Run this StringLengthClient.php to receive the service.

So the output will be:

Returning value of __soapCall() call: Length of the string lakmali is : 7
Dumping request headers:
POST /simple_server/StringLengthServer.php HTTP/1.1
Host: localhost
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.2.9
Content-Type: text/xml; charset=utf-8
SOAPAction: "urn:urn://"
Content-Length: 510

Dumping request:

<soap-env:envelope soap-env:encodingstyle="" xmlns:ns1="urn:urn://" xmlns:soap-enc="" xmlns:soap-env="" xmlns:xsd="" xmlns:xsi="">
 <param0 xsi:type="xsd:string">lakmali</param0>

Dumping response headers:
HTTP/1.1 200 OK
Date: Tue, 07 Jun 2011 13:24:06 GMT
Server: Apache/2.2.10 (Fedora)
X-Powered-By: PHP/5.2.9
Content-Length: 550
Connection: close
Content-Type: text/xml; charset=utf-8

Dumping response:

<soap-env:envelope soap-env:encodingstyle="" xmlns:ns1="urn://" xmlns:soap-enc="" xmlns:soap-env="" xmlns:xsd="" xmlns:xsi="">
 <return xsi:type="xsd:string">Length of the string lakmali is : 7</return>
Here the client program is written to display SOAP messages exchanged. But for a real client ony you need is return value from soapCall.

# StringLengthClient.php
# Copyright (c) 2011 by Lakmali Baminiwatta
   $client = new SoapClient(null, array(
      'location' => "http://localhost/simple_server/StringLengthServer.php",
      'uri'      => "urn:urn://",
      'trace'    => 1 ));

   $return = $client->__soapCall("findLength",array("lakmali"));
   echo("\nResult: ".$return);


  1. Awesome post Lakmali! This really helped me in debug mode. Also rare to find any documentation with no WSDL

  2. Hi lakmali
    Dont u hv PHP-SOAP web service with a WSDL example because we try to access wsdl web service in DSS using PHP.

    1. Check this link, you'll find what you need.

  3. Thank you sharing this kind of noteworthy information. Nice Post.


  4. Good job! Fruitful article. I like this very much. It is very useful for my research. It shows your interest in this topic very well. I hope you will post some more information about the software. Please keep sharing!!
    German Classes in Chennai
    German Language Classes in Chennai
    German Courses in Chennai
    German Language Course in Chennai
    Angularjs Training in Chennai
    Selenium Training in Chennai
    Software Testing Training in Chennai
    German classes in Anna Nagar


Post a Comment

Popular posts from this blog

How to add Maven dependency from a relative path referencing a local jar

Manually install a maven dependency

How to invoke APIs in SOAP style in Swagger