3 Ways to consume SOAP Web Service in PHP

Here I am going to show you how to consume SOAP web service in PHP in 3 ways. To consume SOAP webservice you must have a SOAP web service deployed onto a server. Here I am going to consume or call the SOAP service which is ready made available on the internet. I am calling the CelsiusToFahrenheit which converts temperature from celsius to fahrenheit.

You may also create your own service and write a SOAP client to consume the service. The method of consumption is similar to what I am going to show you here. The WSDL is available at the link https://www.w3schools.com/xml/tempconvert.asmx?WSDL.

Related Posts:


Apache HTTP Server 2.4, PHP 7.4.3, NUSOAP Library, CURL

Configure PHP SOAP

Consume SOAP Service

I will show you here 3 different ways of calling or consuming SOAP web service. The first method is using SoapClient, the second method is using NUSOAP library and the third method is using CURL.

Using SoapClient

Using SoapClient you do not need to use any third party library because SoapClient is already available in PHP engine.

The following PHP code using SoapClient calls the celsius to fahrenheit converter method and will give you the temperature in fahrenheit for the given input in celsius.

$wsdl   = 'https://www.w3schools.com/xml/tempconvert.asmx?WSDL';
$client = new SoapClient($wsdl, array('trace'=>1));  // The trace param will show you errors

$input_celsius = 36;
// web service input param
$request_param = array(
    'Celsius' => $input_celsius

try {
    $responce_param = $client->CelsiusToFahrenheit($request_param);
	echo $input_celsius . ' Celsius => ' . $responce_param->CelsiusToFahrenheitResult . ' Fahrenheit';
} catch (Exception $e) { 
    echo "<h2>Exception Error</h2>"; 
    echo $e->getMessage(); 


This is a third party library and using it you can consume SOAP service. The library is shipped with the source code for this example and you can download from the bottom of this tutorial in source code section.


$wsdl   = "https://www.w3schools.com/xml/tempconvert.asmx?WSDL";
$client = new nusoap_client($wsdl, 'wsdl');

$action = "CelsiusToFahrenheit"; // webservice method name

$result = array();

$input_celsius = 36;
$input = '<CelsiusToFahrenheit xmlns="https://www.w3schools.com/xml/"><Celsius>' . $input_celsius . '</Celsius></CelsiusToFahrenheit>';

if (isset($action))
    $result['response'] = $client->call($action, $input);

//echo $result['response']['CelsiusToFahrenheitResult'];
echo $input_celsius . ' Celsius => ' . $result['response']['CelsiusToFahrenheitResult'] . ' Fahrenheit';

Using CURL

Using CURL command you can also call SOAP service as shown in the following example.

In the following example notice I have used only web service URL instead of WSDL URL but in the above two examples I had used WSDL URL.

$webservice_url = "https://www.w3schools.com/xml/tempconvert.asmx";

$input_celsius = 36;

$request_param = '<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
    <CelsiusToFahrenheit xmlns="https://www.w3schools.com/xml/">
      <Celsius>' . $input_celsius . '</Celsius>

$headers = array(
    'Content-Type: text/xml; charset=utf-8',
    'Content-Length: '.strlen($request_param)

$ch = curl_init($webservice_url);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $request_param);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$data = curl_exec ($ch);

$result = $data;

if ($result === FALSE) {
    printf("CURL error (#%d): %s<br>\n", curl_errno($ch),

curl_close ($ch);

echo $input_celsius . ' Celsius => ' . $data . ' Fahrenheit';

Testing the Application

Make sure your Apache HTTP Server is up and running. Now you can execute any of the above code and for each example you will see the following example on the browser.

consume soap web service in php

That’s all, hope you got an idea how to call or consume SOAP web service using PHP programming language.

Source Code


1 thought on “3 Ways to consume SOAP Web Service in PHP

Leave a Reply

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