How to create and consume SOAP Web Service using SoapServer and SoapClient in PHP

In this tutorial I am going to show you how to create SOAP web service using SoapServer and how to consume web service using SoapClient in PHP. I am going to create a WSDL or web service definition language file for defining the contract between the server and client.

SoapServer is used to create server side implementation of the SOAP service, whereas SoapClient is to used to consume the SOAP service.

Related Posts:

Prerequisites

Apache HTTP Server 2.4, PHP 7.4.3

PHP SOAP Configuration

Project Directory

It’s assumed that you have setup Apache and PHP in Windows system.

Now I will create a project root directory called php-create-soap-service under the Apache server’s htdocs folder.

I may not mention the project root directory in subsequent sections and I will assume that I am talking with respect to the project root directory.

WSDL File

The WSDL (Web Service Definition Language) file defines the contract between client and server and it is both platform and language independent.

The following WSDL file contains the required semantics, such as, request, response, operation, port.

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

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

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

    <portType name="FahrenheitPort">
        <operation name="celsiusToFahrenheit">
            <input message="tns:FahrenheitRequest"/>
            <output message="tns:FahrenheitResponse"/>
        </operation>
    </portType>

    <binding name="FahrenheitBinding" type="tns:FahrenheitPort">
        <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="celsiusToFahrenheit">
            <soap:operation soapAction="urn:FahrenheitAction"/>
            <input>
                <soap:body use="encoded" namespace="urn:CToF" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
            </input>
            <output>
                <soap:body use="encoded" namespace="urn:CToF" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
            </output>
        </operation>
    </binding>

    <service name="WSDLService">
        <port name="FahrenheitPort" binding="tns:FahrenheitBinding">
            <soap:address location="http://localhost/php-create-soap-service/soap-create-soapserver.php"/>
        </port>
    </service>
</definitions>

WSDL file located at the link http://localhost/php-create-soap-service/soap-create-soapserver.php?wsdl.

This is what you will get on the browser:

create consume soap webservice php

SOAP Server

I am creating a simple SOAP server that has a function which will convert the temperature unit into Fahrenheit for the given Celsius unit.

I am loading the contract in the SoapServer constructor. Then I am registering the function.

<?php

ini_set('soap.wsdl_cache_enabled', '0');

function celsiusToFahrenheit($celsius) {
	$fahrenheit = $celsius * 9 / 5 + 32;

	return $fahrenheit;
}

// initialize SOAP Server
$server = new SoapServer('ctof.wsdl');

// register available function
$server->addFunction('celsiusToFahrenheit');

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

?>

SOAP Client

I am also creating a SoapClient that will consume the above service. In the SoapClient constructor you need to pass the WSDL file’s location.

<?php

echo 'Create and Consume SOAP Service using PHP';
echo nl2br("\n\n");

$client = new SoapClient( 'http://localhost/php-create-soap-service/ctof.wsdl' );

try {
	$response = $client->celsiusToFahrenheit( 36 );
	echo 'Celsius to Fahrenheit: ' . $response;
} catch ( SoapFault $sf ) {
	//echo $sf;
	echo 'Error:: ' . $sf->getMessage();
}

?>

Testing the Application

When you run the soap-service-client.php in the browser by hitting the URL http://localhost/php-create-soap-service/soap-service-client.php then you will see the following output on the browser.

create and consume soap web service using soapserver and soapclient in php

That’s all how to create and consume SOAP webservice in PHP programming language.

Source Code

Download

Leave a Reply

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