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

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

  1. codes.tools.asitavsen.com Avatar

    References:

    Buy legal steroids in usa

    References:
    https://git.meshel.cn/brentonscobie

  2. git.warze.org Avatar

    References:

    The adverse effects of using anabolic steroids are serious because

    References:
    http://yun.pashanhoo.com:9090/jeanninebrazil

  3. 123.57.146.97 Avatar

    References:

    Are strongmen on steroids

    References:
    http://112.74.106.216:3000/jamiecarrol246

  4. git.huwhy.cn Avatar

    References:

    Why are steroids bad for athletes

    References:
    http://git.youkehulian.cn/rebbecawellish

  5. skillrizen.com Avatar

    References:

    What supplements have steroids in them

    References:
    http://119.3.186.224:3000/jaclynalston8

  6. git.meshel.cn Avatar

    References:

    Pro bodybuilder steroids

    References:
    https://gitea.yantootech.com/denishaabe7004

  7. 59.19.179.48 Avatar

    References:

    Prohormone vs steroids

    References:
    https://gitea.scivigi.com/heike07779280

  8. git.hubhoo.com Avatar

    References:

    Prednisone alternatives

    References:
    http://121.41.95.54:3000/gabrielz775592

  9. git.anagora.org Avatar

    References:

    Do legal anabolic steroids work

    References:
    https://gitea.alexandermohan.com/maricruzv0247

  10. z.duowenlvshi.com Avatar

    References:

    Anabolic steroid athletes

    References:
    http://74.48.174.77:3000/norbertoskille

  11. https://fanajobs.com/ Avatar

    References:

    Buying anabolic steroids online

    References:
    https://git.game-dev.space/candice9233023

  12. 121.43.244.209 Avatar

    References:

    Anabolic steroids price

    References:
    https://bigotube.com/@amelieworley87?page=about

  13. 221.226.114.142 Avatar

    References:

    Cheapest steroid cycle

    References:
    https://62.234.182.183/emilhayworth43

  14. www.51kimi.cn Avatar

    References:

    Most effective steroids

    References:
    http://82.156.121.2:3000/liamfurst75766

  15. uotshhqppl Avatar

    getdonxegziunfxwnoldrhnwwlmgui

  16. chinami.com Avatar

    References:

    Artificial testosterone

    References:
    https://git.paulll.cc/careyhardee368

  17. git.4lcap.com Avatar

    References:

    Best injectable steroids for mass

    References:
    http://175.154.160.23:3237/jessicahedley

  18. gitea.shirom.me Avatar

    References:

    What is a legal steroid

    References:
    https://music.1mm.hk/lakeshakosovic

  19. www.bongmedia.tv Avatar

    References:

    Testosterone shots for bodybuilding

    References:
    https://gitea.shizuka.icu/elmomccary3958

  20. nauticauruguay.com Avatar

    References:

    Best supplements for bodybuilding without side
    effects

    References:
    http://43.143.175.54:3000/karl87q7958080

  21. https://git.zakum.cn/ Avatar

    References:

    How to take anadrol properly

    References:
    https://mygit.kikyps.com/roxanneq704325

  22. 101.42.158.231 Avatar

    References:

    How to get big fast without steroids

    References:
    http://111.230.243.127:3000/fallonpulsford

  23. istihdam.efeler.bel.tr Avatar

    References:

    Steroid cycles for beginners

    References:
    http://crontab.club:30015/jerrolddortch2

  24. 121.41.2.71 Avatar

    References:

    Anabolic steroid cream

    References:
    https://gitea.my-intrudair.com/jettdoolittle

  25. https://git.ccmhub.se/ Avatar

    References:

    Steroids before and after 3 months

    References:
    http://1.95.7.169:3000/emiliabaxley0

  26. www.workerrenter.pro Avatar

    References:

    Gnc supplements with steroids

    References:
    https://git.game-dev.space/brookeceja9195

  27. git.zxkedu.com Avatar

    References:

    Legal steroids free trial

    References:
    https://ccn-tv.news/@janicousin2470?page=about

Leave a Reply

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