Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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>
@SLSA-IT

This comment has been minimized.

Copy link

@SLSA-IT SLSA-IT commented Mar 5, 2017

Have changed the URL and it works like a charm. Thanks for your sharing.

@uxen-ab

This comment has been minimized.

Copy link

@uxen-ab uxen-ab commented Jan 26, 2018

Have changed the URL in test.wsdl and client.php to match with my server's URL. It works great, thanks.

@icemalik

This comment has been minimized.

Copy link

@icemalik icemalik commented Feb 3, 2018

Thanks for you!

@zamansandhu

This comment has been minimized.

Copy link

@zamansandhu zamansandhu commented Feb 6, 2018

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

This comment has been minimized.

Copy link

@budgrib budgrib commented Feb 20, 2018

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

@budgrib

This comment has been minimized.

Copy link

@budgrib budgrib commented Feb 20, 2018

by the way... Thank You!

@yenhtp-fpt

This comment has been minimized.

Copy link

@yenhtp-fpt yenhtp-fpt commented Jun 17, 2018

Thank you!

@viniciusnm

This comment has been minimized.

Copy link

@viniciusnm viniciusnm commented Jun 18, 2018

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

@ggo98

This comment has been minimized.

Copy link

@ggo98 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

This comment has been minimized.

Copy link

@nsarvar nsarvar commented Feb 8, 2019

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

@mohamedhamdy5

This comment has been minimized.

Copy link

@mohamedhamdy5 mohamedhamdy5 commented Mar 27, 2019

Thank you

@ashokanjj

This comment has been minimized.

Copy link

@ashokanjj ashokanjj commented Apr 22, 2019

Good. Thank you

@somdeesom

This comment has been minimized.

Copy link

@somdeesom somdeesom commented May 21, 2019

thank you so much

@somdeesom

This comment has been minimized.

Copy link

@somdeesom somdeesom commented May 21, 2019

Hello sir thank you you Demo

@alsaleem00

This comment has been minimized.

Copy link

@alsaleem00 alsaleem00 commented Oct 21, 2019

thank you for your demo.

@alexandertre

This comment has been minimized.

Copy link

@alexandertre alexandertre commented Dec 25, 2019

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

@Nasirinezhad

This comment has been minimized.

Copy link

@Nasirinezhad Nasirinezhad commented Jan 20, 2020

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

This comment has been minimized.

Copy link

@tothpaul 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".

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