Call Soap Api with PHP SoapClient and Curl – Parse XML


Important term in soap PHP

  • SoapClient
  • WSDL
  • SimpleXML Extension
  • simplexml_load_string()

SoapClient :

  • SoapClient class is the simplest way of calling soap api. We instantiate this class object and pass the soap api URL or WSDL URL into it.
  • To use SoapClient in PHP, your PHP version should have a SOAP module installed.If this module is not present then you will get fatal error like = “Fatal error: Class ‘SoapClient’ not found in /var/www/html/soap.php on line 8”.
  • If you are using Linux and you want to check that what PHP modules are installed, then use command = php -m
  • To install soap module in Linux distribution CENTOS then use command = yum install php-soap.
  • Don’t forget to restart the Apache server after installing the soap module.
  • Below is the example to create soapclient class object and call soap api using soap url or wsdl url.


$client = new SoapClient(wsdl url);

$response = $client->studentdetail(roll_no);



WSDL(Web Service Definition Language)

  • There are two mode on which soap api relies => WSDL and Non WSDL mode. Here we are discussing about WSDL mode.
  • For Soap API with WSDL mode, WSDL file link is required which is passed as API url in SoapClient, while instantiating a SoapClient object.
  • Wsdl file contains all the operations or functions which we can call and get the response using wsdl url.
  • If we have soap api url, we can append “?wsdl”  to it and then hit that url in the browser,we will get the wsdl file description with all the operations or functions along with the parameters which needs to be passed to get response from those function.

SimpleXML Extension

  • Although SoapClient gives the response in the form of object but sometimes that object also contains XML within it.So to parse and convert that XML into simpleXMLElement Object we need a php extension or module known as simpleXML. This module activates XML to object converter function which is simplexml_load_string().
  • In linux, to check if SimpleXML module exist in your php version then use command = php -m.
  • If this extension or module is not installed, you will get the error like = “Fatal error: Call to undefined function simplexml_load_string() in /var/www/html/soap.php on line 20”.
  • To install this extension in linux Centos use command = yum install php-xml.
  • Don’t forget to restart the Apache server after installing this module.
  • After installing this module we can use a function as $xml = simplexml_load_string()  to convert XML into object.Then we need to use below expression to convert it into array


  • SimpleXML extension works with PHP 5 and above version.


  • This function is used to convert an XML string into an object.
  • It can only be used when simpleXML extension is installed in the system.

Real Example of calling Soap API using PHP SoapClient.

  • I am using php version 5.6.
  • In SoapClient, we don’t have to pass the request as XML.We simply should know that which operation or function we need to call from wsdl along with the parameters which needs to be passed.
  • Using sample WSDL file
  • If you open the above wsdl url in the browser, you will get a XML file which contains all the function we can use from this soap api.
  • I am using NumberToWords() function from the above WSDL file.This function takes a number as an argument and converts that number into words.For example if you pass “100” as an argument, the response will be “one hundred”.
$client = new SoapClient("");
$response = $client->NumberToWords(array('ubiNum'=>100));

Real Example of calling Soap API using PHP and Curl

Using the same URL and parameter as used above, calling the Soap API with Curl

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "",
  "<soapenv:Envelope xmlns:soapenv=\"\" xmlns:web=\"\">\n  
  CURLOPT_HTTPHEADER => array("content-type: text/xml"),
$response = curl_exec($curl);
$err = curl_error($curl);
if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;

Testing Soap API

We can test Soap API either using PostMan or using SoapUI application.

To test using Postman follow below steps

1. Use method as POST.
2. In URL pass the soap api url or wsdl url(
3. In the Header section use => Content-Type : text/xml
4. Pass below XML request in Body section as raw data
<soapenv:Envelope xmlns:soapenv="" xmlns:web="">

I want to Thanks for this nice content.

Please Post Your Comments & Reviews

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