This is a short guide on how to use a local WSDL file with the PHP SoapClient object. In this guide, I will also show you how to configure SoapClient to send your requests to an external service.
I will admit that I am not the most knowledgeable when it comes to SoapClient and web services, which is why this particular issue caught me out one day. It seems as though I came into the development world just as REST APIs were beginning to replace web services.
Save the WSDL file to a local path.
Firstly, you will need to download the WSDL file to a local path. Note that you will probably need a flattened WSDL as PHP’s SoapClient can have issues loading external entities.
For the sake of this tutorial, I will assume that you saved the WSDL file in the same directory as your Soap Client and that you called it Service.xml.
Here’s the code:
<?php //The FULL path to the location of your WSDL file. $fullPathToWsdl = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Service.xml'; //The URL of the web service. $url = 'http://test.service.com/'; //Instantiate the SoapClient object. $client = new SoapClient( $fullPathToWsdl, array( 'location' => $url, 'trace' => 1 ) );
In the code above, we:
- Passed the full path of the WSDL file in as the first parameter to SoapClient.
- We set the location option in the second parameter as the web service URL.
Hopefully, this guide saved you some time and hardship!