Call Soap Api with PHP SoapClient and Curl – Parse XML

Last Updated on

INDEX OF THIS BLOG

  1. List of Important Terms Used To implement Soap API.
  2. Explanation of each term for better understanding.
  3. Real Example of calling Soap API using PHP SoapClient.
  4. Real Example of calling Soap API using PHP and Curl.
  5. How to test Soap API using tools like POSTMAN and SOAPUI.

List of Important Terms Used for Calling Soap API Using SoapClient

  • SoapClient
  • WSDL
  • SimpleXML Extension
  • simplexml_load_string()

Before making a call to soap api lets understand the above terms one by one.

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.

<?php

$client = new SoapClient(wsdl url);

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

print_r($response);

?>

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

       $array=json_decode(json_encode((array)$xml),true);

  • SimpleXML extension works with PHP 5 and above version.

simplexml_load_string()

  • 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 http://www.dataaccess.com/webservicesserver/numberconversion.wso?WSDL
  • 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”.

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

 

Testing Soap API

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

To test using Postman follow below steps

 

 

One thought on “Call Soap Api with PHP SoapClient and Curl – Parse XML

Leave a Reply

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