We will cover below topics:-
- List of Important Terms Used To implement Soap API.
- Explanation of each term for better understanding.
- Real Example of calling Soap API using PHP SoapClient.
- Real Example of calling Soap API using PHP and Curl.
- 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 https://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”.
1 2 3 4 5 6 7 8 9 10 |
<?php //ini_set('display_errors','On'); $client = new SoapClient("https://www.dataaccess.com/webservicesserver/numberconversion.wso?WSDL"); $response = $client->NumberToWords(array('ubiNum'=>100)); print_r($response); ?> |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "https://www.dataaccess.com/webservicesserver/numberconversion.wso?WSDL=", CURLOPT_RETURNTRANSFER => true, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POSTFIELDS => "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://www.dataaccess.com/webservicesserver/\">\n <soapenv:Header/>\n <soapenv:Body>\n <web:NumberToWords>\n <ubiNum>100</ubiNum>\n </web:NumberToWords>\n </soapenv:Body>\n </soapenv:Envelope>", CURLOPT_HTTPHEADER => array("content-type: text/xml"), )); $response = curl_exec($curl); $err = curl_error($curl); curl_close($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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
1. Use method as POST. 2. In URL pass the soap api url or wsdl url(https://www.dataaccess.com/webservicesserver/numberconversion.wso?WSDL). 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="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.dataaccess.com/webservicesserver/"> <soapenv:Header/> <soapenv:Body> <web:NumberToWords> <ubiNum>100</ubiNum> </web:NumberToWords> </soapenv:Body> </soapenv:Envelope> |
Excellent Keep up the good work. This explanation is impressive!
I’m very lucky to find your example that help get a response from a SOAP webservice.
Many thanks for your great tutorial.
my question is how can I parse the curl response to save each of the result into a database ?
I’m lucky to encounter this tutorial that save me from a long time searching a way to get response to a SOAP webservice with php. I was trying to do it with SOAP client of PHP, I failed, but I made it with your curl example. Many thanks.
My question is how can I parse the curl response to get result separately to save each of them into a mysql database ?
If you get a response in xml then use simplexml_load_string() function (explanation is given above) to convert the response into an object or if you get a response in JSON format then use json_decode() function(explanation is provided above).
Thank you for your clear and easy to understand explanation.