Skip to content

Instantly share code, notes, and snippets.

@umidjons
Last active July 25, 2024 07:45
Show Gist options
  • Save umidjons/f3de2533c51495a9c557 to your computer and use it in GitHub Desktop.
Save umidjons/f3de2533c51495a9c557 to your computer and use it in GitHub Desktop.
Simple Web service - SOAP Server/Client in PHP

Simple Web service - SOAP Server/Client in PHP

Implementation of the SOAP server - server.php:

<?php
// turn off WSDL caching
ini_set("soap.wsdl_cache_enabled","0");

// model, which uses in web service functions as parameter
class Book
{
	public $name;
	public $year;
}

/**
 * Determines published year of the book by name.
 * @param Book $book book instance with name set.
 * @return int published year of the book or 0 if not found.
 */
function bookYear($book)
{
	// list of the books
	$_books=[
		['name'=>'test 1','year'=>2011],
		['name'=>'test 2','year'=>2012],
		['name'=>'test 3','year'=>2013],
	];
	// search book by name
	foreach($_books as $bk)
		if($bk['name']==$book->name)
			return $bk['year']; // book found

	return 0; // book not found
}

// initialize SOAP Server
$server=new SoapServer("test.wsdl",[
	'classmap'=>[
		'book'=>'Book', // 'book' complex type in WSDL file mapped to the Book PHP class
	]
]);

// register available functions
$server->addFunction('bookYear');

// start handling requests
$server->handle();

Implementation of the SOAP client - client.php:

<?php
// model
class Book
{
	public $name;
	public $year;
}

// create instance and set a book name
$book      =new Book();
$book->name='test 2';

// initialize SOAP client and call web service function
$client=new SoapClient('http://a.uz/soap/server.php?wsdl',['trace'=>1,'cache_wsdl'=>WSDL_CACHE_NONE]);
$resp  =$client->bookYear($book);

// dump response
var_dump($resp);

Dump of the response:

string '2012' (length=4)

WSDL file test.wsdl:

<?xml version="1.0" encoding="UTF-8"?>

<wsdl:definitions name="Library"
                  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                  targetNamespace="Library"
                  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
                  xmlns:tns="Library"
                  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">

    <xsd:documentation></xsd:documentation>

    <wsdl:types>
        <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="Library">
            <xsd:complexType name="book">
                <xsd:sequence>
                    <xsd:element name="name" type="xsd:string"></xsd:element>
                    <xsd:element name="year" type="tns:integer"></xsd:element>
                </xsd:sequence>
            </xsd:complexType>
        </xsd:schema>
    </wsdl:types>

    <wsdl:message name="bookYearRequest">
        <wsdl:part name="book" type="xsd:book"></wsdl:part>
    </wsdl:message>
    <wsdl:message name="bookYearResponse">
        <wsdl:part name="year" type="tns:integer"></wsdl:part>
    </wsdl:message>

    <wsdl:portType name="Library">
        <wsdl:operation name="bookYear">
            <wsdl:input message="tns:bookYearRequest"/>
            <wsdl:output message="tns:bookYearResponse"/>
        </wsdl:operation>
    </wsdl:portType>

    <wsdl:binding name="Library" type="tns:Library">
        <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="bookYear">
            <soap:operation soapAction="http://a.uz/soap/server.php"/>
            <wsdl:input>
                <soap:body use="literal" namespace="Library"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal" namespace="Library"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>

    <wsdl:service name="Library">
        <wsdl:port binding="tns:Library" name="BookLibrary">
            <soap:address location="http://a.uz/soap/server.php"/>
        </wsdl:port>
    </wsdl:service>

</wsdl:definitions>
@icemalik
Copy link

icemalik commented Feb 3, 2018

Thanks for you!

@zamansandhu
Copy link

got that this error "the page is not working".change the url according to my server http://localhost/soap/server.php?wsdl and http://localhost/soap/client.php. can you kindly help.

@budgrib
Copy link

budgrib commented Feb 20, 2018

When I removed " ,['trace'=>1,'cache_wsdl'=>WSDL_CACHE_NONE] " from the client.php file, it worked

@budgrib
Copy link

budgrib commented Feb 20, 2018

by the way... Thank You!

@DuyHH2-FPT
Copy link

Thank you!

@viniciusnm
Copy link

I can not execute, when I call the client.php file the page returns blank.

@ggo98
Copy link

ggo98 commented Sep 18, 2018

Hello,
thanks for this example.
However, it does not work that well with SoapUI for example, neither with some other products able to access web services.
It doesn't work with Visual Studio also (I can add a Web Service reference, but then the result is wrong).
There might be something in the WSDL, but I didn't investigate up to now...

@nsarvar
Copy link

nsarvar commented Feb 8, 2019

there's a mistake in your wsdl, tns:integer

@mohamedhamdy5
Copy link

Thank you

@ashokanjj
Copy link

Good. Thank you

@somdeesom
Copy link

thank you so much

@somdeesom
Copy link

Hello sir thank you you Demo

@alsaleem00
Copy link

thank you for your demo.

@alexandertre
Copy link

Hello! If change «bookYear» in wsdl, in function name and in client call, I got error. Correct work just with «bookYear». Why?

@Nasirinezhad
Copy link

hi, thank you first
i copied that and tried to test on wamp server, client takes a while ro response and at the end return this

Warning: Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://phpwww/serviceClient/index.php?wsdl' : failed to load external entity "http://phpwww/serviceClient/index.php?wsdl" in E:\WorkSpace\Php\www\serviceClient\index.php:14 Stack trace: #0 E:\WorkSpace\Php\www\serviceClient\index.php(14): SoapClient->SoapClient('http://phpwww/s...', Array) #1 {main} thrown in E:\WorkSpace\Php\www\serviceClient\index.php on line 14

@tothpaul
Copy link

tothpaul commented Aug 8, 2020

they are few errors in the WSDL file, replace everywhere "tns:integer" by "xsd:integer" and "xsd:book" by "tns:book".

@luciendgolden
Copy link

The WSDL file contains errors, you can replace it with the following

<?xml version="1.0" encoding="UTF-8"?>

<wsdl:definitions name="Library"
                  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                  targetNamespace="Library"
                  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
                  xmlns:tns="Library"
                  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">

    <xsd:documentation></xsd:documentation>

    <wsdl:types>
        <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="Library">
            <xsd:complexType name="book">
                <xsd:sequence>
                    <xsd:element name="name" type="xsd:string"></xsd:element>
                    <xsd:element name="year" type="xsd:integer"></xsd:element>
                </xsd:sequence>
            </xsd:complexType>
        </xsd:schema>
    </wsdl:types>

    <wsdl:message name="bookYearRequest">
        <wsdl:part name="book" type="tns:book"></wsdl:part>
    </wsdl:message>
    <wsdl:message name="bookYearResponse">
        <wsdl:part name="year" type="xsd:integer"></wsdl:part>
    </wsdl:message>

    <wsdl:portType name="Library">
        <wsdl:operation name="bookYear">
            <wsdl:input message="tns:bookYearRequest"/>
            <wsdl:output message="tns:bookYearResponse"/>
        </wsdl:operation>
    </wsdl:portType>

    <wsdl:binding name="Library" type="tns:Library">
        <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="bookYear">
            <soap:operation soapAction="http://a.uz/soap/server.php"/>
            <wsdl:input>
                <soap:body use="literal" namespace="Library"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal" namespace="Library"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>

    <wsdl:service name="Library">
        <wsdl:port binding="tns:Library" name="BookLibrary">
            <soap:address location="http://a.uz/soap/server.php"/>
        </wsdl:port>
    </wsdl:service>

</wsdl:definitions>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment