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

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.


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?

<?xml version="1.0" encoding="UTF-8" ?>
<definitions name="Entities"
            targetNamespace="urn:blog"
            xmlns:typens="urn:blog"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
            xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
            xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
            xmlns="http://schemas.xmlsoap.org/wsdl/">
    <types>
        <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:blog">
        </xsd:schema>
    </types>

    <message name="getItemResponse">
        <part name="string" type="xsd:string" />
    </message>

     <message name="getItemRequest">
        <part name="return" type="xsd:string" />
    </message>

    <portType name="blogPortType">
        <operation name="getItem">
            <documentation>Returns a simple test string</documentation>
            <input message="typens:getItemRequest" />
            <output message="typens:getItemResponse" />
        </operation>
    </portType>

    <binding name="blogBinding" type="typens:blogPortType">
        <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>

        <operation name="getItem">
            <soap:operation soapAction="urn:blog#getItem" style="rpc" />
            <input>
                <soap:body use="encoded" namespace="urn:blog" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
            </input>
            <output>
                <soap:body use="encoded" namespace="urn:blog" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
            </output>
        </operation>
        
    </binding>

    <service name="blog">
        <port name="blogPort" binding="typens:blogBinding">
            <soap:address location="http://{siteUrl}/webservice/server.php" />
        </port>
    </service>
</definitions>

As you can see the WSDL consists of different parts:

– the namespace
– the types
– messages
– ports (the porttype contains the set of operations, each operation has its own input and output messages)
– bindings (tells how to transport the requests, it describes the protocol used, just like the ports it contains the set of operations used)
– service (defines the ports that are supported by the service, its a collection of ports)

The third step, map our Blog service class to the webserver

After we have created our WSDL we can start building our server. In just a few lines we have a SOAP server up and running.

 // Init the server
$oServer = new SoapServer('blog.wsdl');

// Register the blog service class and all the methods
$oServer->setClass('BlogService');

// Rock 'n roll
$oServer->handle();

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:

ini_set("soap.wsdl_cache_enabled", "0"); // Disable the wsdl cache

$oClient = new SoapClient(
				// Url to our wsdl, http://{siteUrl}/webservice/index.php?wsdl is also possible
				'{siteUrl}/webservice/blog.wsdl', 
				array(
					'trace'  => true,
					'exceptions'=> true
				));

try {
	$aResult = $oClient->getItem(4);
	var_dump($aResult);
} catch (SoapFault $e) {
	exit($e->faultstring);
}

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

Comments

183 responses to “Introduction into SOAP, setting up a simple webservice with PHP SOAP”

  1. shrooms delivery Avatar

    I think that is one of the most vital info for me.
    And i am satisfied reading your article. However wanna statement on some
    normal issues, The website style is ideal,
    the articles is in reality excellent : D. Just
    right job, cheers

  2. buy weed online in canada Avatar

    I’m no longer sure where you’re getting your info, but good
    topic. I must spend a while finding out more or working out
    more. Thanks for magnificent information I used to be in search
    of this information for my mission.

  3. binance Konto er"offnen Avatar

    Your article helped me a lot, is there any more related content? Thanks!

  4. Kristian Wilgocki Avatar

    $0.50 Per Minute Live Chat Job. This is an online job as a live chat assistant where you get paid per minute. If you login and do 15 minutes work and then stop, you are paid immediately for those 15 minutes of work. The per minute pay rate for these live chat assistant jobs is $0.50 per minute. So if you login and do 15 minutes work, you get paid $7.50. If you login and do 3 hours work you are paid $90. There are a lot of businesses hiring for these jobs from all over the world so you can set your own hours and work as much or as little as you want per week. You can check more info here : http://chat-assistant.advertising4you.co.uk

  5. drover sointeru Avatar

    Thanks for every other informative website. The place else may just I get that kind of info written in such a perfect way? I’ve a undertaking that I’m simply now working on, and I’ve been on the glance out for such info.

  6. Winford Moeckel Avatar

    I?¦ll immediately seize your rss feed as I can’t in finding your e-mail subscription link or e-newsletter service. Do you have any? Please permit me know in order that I could subscribe. Thanks.

  7. Steven Luther Avatar

    $190/day YouTube unboxing job ! I want to offer you this $190/day YouTube job. But first I must check that you can perform the basic tasks required. So, do you think you can handle this four-step process? Download premade video content we’ll send you. Upload the video content to the YouTube account we’ll provide you with. Input title for the video we provide you with. Receive payment of $35-$50 per video. I hope that seems manageable to you because we’re ready to get you set up with this job right now. You can find more info here: http://social-media-jobs.advertising4you.co.uk

  8. registrarse en www.binance.com Avatar

    I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article. https://www.binance.com/register?ref=IHJUI7TF

  9. gelatin trick Avatar

    Hi, I do think this is a great blog. I stumbledupon it 😉 I will come back once again since i have
    saved as a favorite it. Money and freedom is the greatest way to change, may you be rich
    and continue to guide others.

  10. gelatin trick Avatar

    Everything is very open with a really clear explanation of the issues.
    It was truly informative. Your site is extremely helpful.
    Thank you for sharing!

  11. gelatin recipe for weight loss Avatar

    Thank you for the good writeup. It in fact was a amusement account it.
    Look advanced to far added agreeable from you! By the way, how can we communicate?

  12. gelatin weight loss trick Avatar

    Hello, I enjoy reading through your post. I like to write a little comment to support you.

  13. gelatin weight loss Avatar

    Just want to say your article is as surprising. The clearness in your post is simply excellent and i
    could assume you are an expert on this subject. Well with your permission let me to grab
    your RSS feed to keep updated with forthcoming post. Thanks a million and
    please carry on the rewarding work.

  14. huishoudrobot Avatar

    Undeniably believe that which you said. Your favorite reason appeared to be on the internet the easiest thing to be aware of. I say to you, I definitely get irked while people consider worries that they just don’t know about. You managed to hit the nail upon the top and defined out the whole thing without having side-effects , people could take a signal. Will probably be back to get more. Thanks

  15. gelatin trick recipe Avatar

    Wow, fantastic weblog layout! How lengthy have you ever been running a blog for?
    you made blogging glance easy. The whole glance of
    your web site is fantastic, let alone the content!

  16. buy Instagram followers Avatar

    I loved as much as you’ll receive carried out right here.
    The sketch is attractive, your authored subject matter stylish.
    nonetheless, you command get got an impatience over that you wish be delivering the following.
    unwell unquestionably come more formerly again since exactly the same
    nearly a lot often inside case you shield this hike.

  17. zabornatorilon Avatar

    hi!,I like your writing very much! share we communicate more about your post on AOL? I need an expert on this area to solve my problem. May be that’s you! Looking forward to see you.

  18. polototo togel Avatar

    There is visibly a bundle to know about this. I feel you made some nice points in features also.

  19. startek bv koop online Avatar

    Thank you for the good writeup. It in fact was a amusement account it. Look advanced to more added agreeable from you! However, how could we communicate?

  20. robot leverancier Avatar

    Nice blog right here! Additionally your site rather a lot up fast! What web host are you the use of? Can I am getting your affiliate link in your host? I wish my site loaded up as quickly as yours lol

  21. studyleo.com Avatar

    bookmarked!!, I really like your site!

  22. studyleo Avatar

    Thank you for another informative website. The place else could I am getting
    that type of information written in such a
    perfect approach? I have a challenge that I’m just now working on, and I’ve been at the look out for such information.

  23. universities Avatar

    Hello There. I found your blog using msn. This
    is a really well written article. I’ll make sure to
    bookmark it and come back to read more of your useful info.
    Thanks for the post. I’ll certainly comeback.

  24. official here Avatar

    I used to be able to find good information from your
    blog posts.

  25. apply Avatar

    When someone writes an article he/she maintains the thought of a user in his/her
    mind that how a user can be aware of it. So that’s why this
    piece of writing is amazing. Thanks!

  26. study in turkey Avatar

    Thanks for your personal marvelous posting! I certainly enjoyed reading it, you are a great author.
    I will ensure that I bookmark your blog and will often come back from now on. I want to encourage one to
    continue your great posts, have a nice holiday weekend!

  27. ai girlfriend chat Avatar

    What a data of un-ambiguity and preserveness of precious knowledge regarding unexpected feelings.

  28. www.binance.info sign up Avatar

    I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article. https://accounts.binance.info/ro/register-person?ref=HX1JLA6Z

  29. zumospin review Avatar

    Uitgebreide recensie van Zumospin Casino: licentie, spelaanbod, bonussen en mobiele app voor spelers uit Nederland. Welkomstbonus tot €150 + 200 spins.

  30. fdertol mrtokev Avatar

    I’m not sure where you’re getting your information, but great topic. I needs to spend some time learning much more or understanding more. Thanks for great information I was looking for this info for my mission.

  31. 4d result net Avatar

    I’d like to thank you for the efforts you have put in penning this site.
    I am hoping to view the same high-grade content by you in the future as well.
    In fact, your creative writing abilities has motivated me to get my very own site now ;
    )

  32. fdertolmrtokev Avatar

    I’m so happy to read this. This is the kind of manual that needs to be given and not the accidental misinformation that is at the other blogs. Appreciate your sharing this best doc.

  33. garaze do wynajecia Avatar

    I wish to show my affection for your kindness for those individuals that really want guidance on that situation. Your real dedication to passing the message up and down had become amazingly practical and have allowed most people much like me to arrive at their aims. The interesting tips and hints indicates this much to me and substantially more to my colleagues. Many thanks; from everyone of us.

Leave a Reply

Your email address will not be published. Required fields are marked *