Consuming Web Service with PHP SoapClient
What is SOAPclient :
To consume web service using PHP is possible through http://php.net/manual/en/class.soapclient.php‘ target=’_blank’>SoapClient object. PHP 5.0 or more version comes with built in Soapclient class. That provides a client for SOAP 1.1 and SOAP 1.2.
Example of SOAPClient with exception handling:
With below example we can see what are all functions available at Web Server side. After knowing function name with possible parameter we can consume only. Below PHP code cover the same.
<?php /** @Desc: Trying to connect with SOAP wsdl with SOAPClient. If we are able to connect then we can see what are the functions available under this web service and how many parameter each function requires. */ $wsdl = "http://ws.cdyne.com/ip2geo/ip2geo.asmx?wsdl"; $params = array( 'trace' => 1, 'exceptions' => true, 'cache_wsdl' => WSDL_CACHE_NONE, ); try{ $client = new SoapClient($wsdl, $params); } catch( SoapFault $fault) { trigger_error("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR); //echo $client->__getLastRequest ('void');// See what is the last xml send to web service } $functions = $client->__getFunctions(); // Will receive all SOAP function that can be used through that SOAP print_r($functions); //Output //Array ( [0] => ResolveIPResponse ResolveIP(ResolveIP $parameters) [1] => ResolveIPResponse ResolveIP(ResolveIP $parameters) )
How to call Web service functions using SOAPClient:
There are various ways to call a function of web service
- i) $client->__call(‘function_name’,$args);
- ii) $client->__soapCall(‘function_name’, $args);
iii) $client->function_name($args);
We can use any of above way to call web service function. Below code snippet shows same example.
<?php /** @Desc: Call the function of web service. There are various ways to call a function of web service</pre> <ol> <li>i) $client->__call('function_name',$args);</li> <li>ii) $client->__soapCall('function_name', $args);</li> </ol> <pre> iii) $client->function_name($args); */ $wsdl = "http://ws.cdyne.com/ip2geo/ip2geo.asmx?wsdl"; $params = array( 'trace' => 1, 'exceptions' => true, 'cache_wsdl' => WSDL_CACHE_NONE, ); try{ $client = new SoapClient($wsdl, $params); } catch( SoapFault $fault) { trigger_error("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR); //echo $client->__getLastRequest ('void');// See what is the last xml send to web service } try{ $xml = '<ws:ResolveIP> <ws:ipAddress>172.18.122.43</ws:ipAddress> <ws:licenseKey>sasas</ws:licenseKey> </ws:ResolveIP>'; $args = array(new SoapVar($xml, XSD_ANYXML)); $output = $client->__soapCall('ResolveIP',$args); } catch( SoapFault $fault) { echo "this is the request=>".$client->__getLastRequest();// See what is the last xml send to web service //trigger_error("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR); } print_r($output);
Note:By Running this code you will see it doesn’t give proper output . So due to exception handling we will see what is the exact SOAP request made. By seeing the response we can get to know the soap header is not having proper namespace. So in below segment you will see
How to override SOAP envalop header namespace using SOAPclient in PHP
<?php /** How to override SOAP header. Sometimes we don't get proper response due to improper header. Because ideally headers get created automatically and sometimes it might have namespace issue. With this example we will see how to override SOAP envalop header. */ class MSSoapClient extends SoapClient { function __doRequest($request, $location, $action, $version) { $pattern = '/[a-z]+[0-9]+/';// Changing namespace from ns1 to test $replacement = 'ws'; $request = preg_replace($pattern, $replacement, $request); return parent::__doRequest($request, $location, $action, $version); } } $wsdl = "http://ws.cdyne.com/ip2geo/ip2geo.asmx?wsdl"; $params = array( 'trace' => 1, 'exceptions' => true, 'cache_wsdl' => WSDL_CACHE_NONE, ); try{ $client = new MSSoapClient($wsdl, $params); // Here we have overriding SOAPClient class to override soap envalop. } catch( SoapFault $fault) { trigger_error("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR); //echo $client->__getLastRequest ('void');// See what is the last xml send to web service } try{ $xml = '<ws:ResolveIP> <ws:ipAddress>172.18.122.43</ws:ipAddress> <ws:licenseKey>sasas</ws:licenseKey> </ws:ResolveIP>'; $args = array(new SoapVar($xml, XSD_ANYXML)); $output = $client->__soapCall('ResolveIP',$args); } catch( SoapFault $fault) { echo "this is the request=>".$client->__getLastRequest();// See what is the last xml send to web service //trigger_error("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR); } print_r($output);
Now this SOAP service works fine through above code
Free tools to test Web service For windows:
- SOAPUI: SOAPUI is a java based software developed by smartbear. Its freely available for windows as well as linux platform. You can download it from here
- Membrane-client: This is again a generic soap client that provides dynamic form generation based on soap service requirement. It also provide test your web service using SOAP/REST/RAW/FORM/HTTP . So based on our convenience we can test web service call.This SOAP Client software is provided under the terms of the Apache License.
Chandra Shekhar
Latest posts by Chandra Shekhar (see all)
- Best practices for micro service design - January 23, 2022
- Spring Boot - January 23, 2022
- Java - January 23, 2022
Recent Comments