Search code examples
phpmagentosoapmagento-1.5soap-client

Where to put WSDL and how to reference for SoapClient


I'm certain this is a dumb newbie question, but this is the first time I'm working with SOAP, so I guess I qualify. I'm having a heck of a time trying to load a local WSDL file from within my PHP class. I don't understand where it is looking for the file or how I can reference it locally. I could put the absolute path using the full website url, but that seems like overkill and it would ONLY work on this site. I'd prefer to reference the file using a relative path.

I'm using PHP with a Magento store. The class that handles communication with the SOAP server is Namespace_Module_Model_Carrier_Fedex_Addressvalidation, which is located at app/code/local/Namespace/Module/Model/Carrier/Fedex/Addressvalidation.php. An instance of this class is instantiated using Mage::getModel('shippingrates/carrier_fedex_addressvalidation') from within the Namespace_Module_Model_Carrier_Fedex class. The WSDL location is defined at the top of the address validation class:

protected $_wsdl = 'AddressValidationService_v2.wsdl';

Within the constructor of the model I initialize the SoapClient:

$this->_client = new SoapClient($this->_wsdl);
$this->_client->__setLocation($this->getServiceLocation());//returns a remote URL for their test server

The file is located in the same directory as the Addressvalidation.php file, but I have also tried locating it in the same directory as the Fedex.php script as well as at app/code/local/ but nothing works. I have tried setting the WSDL variable to ./AddressValidationService_v2.wsdl as well as various other options but I always get an exception like this:

Fatal error: SOAP-ERROR: Parsing WSDL: Couldn't load from './AddressValidationService_v2.wsdl' : failed to load external entity

If someone can please help me understand the scope and where the SoapClient looks for the files I think I'll have a better understanding of where to put them and how to reference them.


Solution

  • Usually the WSDL file points to the URL not to a local file.

    So if you have a web service set up and running on your local PC, this is how you could set up your client for testing:

    <?php
    
    $wsdl = "http://127.0.0.1/api/service.wsdl";
    
    $options = [
        'trace'      => 1,
        'exceptions' => true,
        'cache_wsdl' => WSDL_CACHE_NONE,
    ];
    
    $client = new SoapClient($wsdl, $options);
    
    $return = $client->getDetails([$id = 1234]);
    
    echo "Request headers: " . print_r($client->__getLastRequestHeaders(), 1), "\n";
    echo "Request: " . $client->__getLastRequest(), "\n";
    echo "Response headers: " . print_r($client->__getLastResponseHeaders(), 1), "\n";
    echo "Response: " . $client->__getLastResponse(), "\n";
    

    This is actually from a working PHP script on my computer. I test my own SoapServer this way and it works. "getDetails" is a method defined in WSDL file and it expects one parameter, which I pass as $id.

    The debugging lines are output after the call, which is very helpful to me.