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.

Requirements

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

Steps

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

So here is the code for StringLengthServer.php

 <?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://www.lakmali.lk"));
  
    $server->addFunction("findLength");
       $server->handle(); 

?>

  • 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
<?php
 StringLengthClient.php
# Copyright (c) 2011 by Lakmali Baminiwatta
#
   $client = new SoapClient(null, array(
      'location' => "http://localhost/simple_server/StringLengthServer.php",
      'uri'      => "urn:urn://www.lakmali.lk",
      'trace'    => 1 ));

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

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

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

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

   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://www.lakmali.lk#findLength"
Content-Length: 510

Dumping request:

<soap-env:envelope soap-env:encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="urn:urn://www.lakmali.lk" xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap-env:body><ns1:findLength>
 <param0 xsi:type="xsd:string">lakmali</param0>
</ns1:findLength></soap-env:body></soap-env:envelope>

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="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="urn://www.lakmali.lk" xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap-env:body><ns1:findLengthResponse>
 <return xsi:type="xsd:string">Length of the string lakmali is : 7</return>
</ns1:findLengthResponse></soap-env:body></soap-env:envelope>
Here the client program is written to display SOAP messages exchanged. But for a real client ony you need is return value from soapCall.


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

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

Comments

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

    ReplyDelete
  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.

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

      http://wso2.com/library/1060

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

    opencu
    Education

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. This comment has been removed by a blog administrator.

    ReplyDelete
  6. This comment has been removed by a blog administrator.

    ReplyDelete

  7. You write this post very carefully I think, which is easily understandable to me. Not only this, but another post is also good. As a newbie, this info is really helpful for me. Thanks to you.
    Tally ERP 9 Training
    tally classes
    Tally Training institute in Chennai
    Tally course in Chennai

    ReplyDelete
  8. Thanks for sharing this valuable information to our vision. You have posted a worthy blog keep sharing.
    Digital Marketing Course In Kolkata
    Web Design Course In Kolkata
    SEO Course In Kolkata

    ReplyDelete
  9. Nice post, I like to read this blog. It is very interesting to read.
    fibonacci in python
    python class inheritance

    ReplyDelete
  10. As I apply more and more of these insights to my daily life I can see how your education has transformed you into a top expert in this industry. I talked about these very points to my professor today and she couldn’t argue any of these points. After seeing this I know I will not be the same. Be cool if you were close to me so we could hang. I don’t know why but this site takes almost a minute to fully load on my friends laptop. Reading your content is way better than snuggles.

    Also visit my homepage ☛ 야설

    ReplyDelete
  11. Don’t be afraid to spread your thoughts 오피헌터 . You have just made my year a great one! You are clearly an influence in this field so I'm sure many of these concepts probably come easily to someone like yourself however, I am still not understanding some things you’ve written here. What a highly descriptive and well written post. Any more feedback? It would be greatly appreciated.

    ReplyDelete
  12. Good web site! I really love how it is simple on my eyes and the data are well written. I am wondering how I might be notified when a new post has been made. I have subscribed to your RSS feed which must do the trick! Have a nice day!

    Please Visit My homepage ➤ 마사지블루

    ReplyDelete
  13. Do you have a spam problem on this blog; I also am a blogger, and I was wanting to know your situation; many of us have created some nice practices and we are looking to exchange solutions with other folks, be sure to shoot me an e-mail if interested. 건마탑

    ReplyDelete
  14. It is very well written, and your points are well-expressed. I request you warmly, please, don’t ever stop writing.
    카지노사이트

    ReplyDelete
  15. Thank you for providing a good quality article.
    토토

    ReplyDelete
  16. I really enjoy your web’s topic. Very creative and friendly for users. Definitely bookmark this and follow it everyday.
    온라인카지노

    ReplyDelete
  17. It is really a great and helpful piece of information. I am glad that you shared this useful info with us. Please keep us informed like this.
    룰렛

    ReplyDelete
  18. Уour blog providеd us useful information to work on. Үou have done a marvelous job! 바카라사이트

    ReplyDelete
  19. You were great and everyone received so much from your experience and knowledge. Absolutely amazing 스포츠토토

    ReplyDelete
  20. This is perfect blog for anyone who is looking for topics like this. It has got it all, information, benefits and overview. A perfect piece of writing. Good job.
    Miracle II Soap Hygiene Products

    ReplyDelete
  21. This is one of the best website I have seen in a long time thank you so much, thank you for let me share this website to all my friends. 블랙잭사이트

    ReplyDelete
  22. This is such a great resource that you are providing and you give it away for free. 토토

    ReplyDelete
  23. Such a great blog.Thanks for sharing......... 경마

    ReplyDelete
  24. Remarkable! Its in fact amazing post, I have got much clear idea
    regarding from this article. 파친코사이트

    ReplyDelete
  25. I have been looking for articles on these topics for a long time. 카지노사이트 I don't know how grateful you are for posting on this topic. Thank you for the numerous articles on this site, I will subscribe to those links in my bookmarks and visit them often. Have a nice day


    ReplyDelete
  26. Hurrah! Finally I got a website from where I be capable of truly take helpful facts regarding my study and knowledge. 바카라사이트

    ReplyDelete
  27. We stumbled over here from a different web address and thought I should check things out. 온라인바둑이

    ReplyDelete
  28. Your post is very interesting to me. Reading was so much fun. I think the reason reading is fun is because it is a post related to that I am interested in. Articles related to 온카지노 you are the best. I would like you to write a similar post about !

    ReplyDelete
  29. Excellent read, Positive site, I have read a few of the articles on your website now, and I really like your style.

    ecommerce catalogue management services

    ecommerce services provider in india

    ReplyDelete
  30. Hi there, I simply hopped over in your website by way of StumbleUpon. Now not one thing I’d typically learn, but I favored your emotions none the less. Thank you for making something worth reading. 먹튀검증업체

    ReplyDelete

Post a Comment

Popular posts from this blog

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

Boomi Mapping - Removing special chars from an input