Magento – Incorrect Location URL returned by V2 SOAP WSDL

apimagento-1.9soap

When fetching the V2 WSDL via this URL:

www.example.com/shop/api/v2_soap?wsdl=1

It is defining the wrong URL for requests:

<service name="MagentoService">
  <port name="Mage_Api_Model_Server_V2_HandlerPort" binding="typens:Mage_Api_Model_Server_V2_HandlerBinding">
    <soap:address location="http://example.com/shop/index.php/?type=v2_soap"/>
  </port>
</service>

As you can see above, it is returning this URL:

www.example.com/shop/index.php/?type=v2_soap

Although it should be:

www.example.com/shop/index.php/api/v2_soap/index/

I've hunted down through a lot of the Magento core code and I believe that value get set in
/app/code/core/Mage/Api/Model/Wsdl/Config/Base.php
around line 53:

    // set up default WSDL template variables
    $this->_wsdlVariables = new Varien_Object(
        array(
            'name' => 'Magento',
            'url'  => htmlspecialchars(Mage::getUrl('*/*/*', array('_query' => $queryParams)))
        )
    );

This part is what appears to be failing:

Mage::getUrl('*/*/*' ...

If I change that to:

Mage::getUrl('api/v2_soap/index' ...

the correct URL is returned.

This question appears to be describing the exact same problem, but there was never an answer for it:

Thanks in advance for any help!

UPDATE

Although I've yet to find a permanent solution to this issue, I have found a temporary solution after searching around a bit. Essentially, I extend the SoapClient class and override the __doRequest method as a means of intercepting requests and tweaking the location URL before the request is sent:

class SoapClientLocation extends \SoapClient
{
    public function __doRequest( $req, $location, $action, $version = SOAP_1_1, $one_way = NULL )
    {
        $location = str_replace( '?type=v2_soap', 'api/v2_soap/index/', $location, $one_way );
        return parent::__doRequest( $req, $location, $action, $version );
    }
}

 $this->client = new SoapClientLocation( $this->config['api']['url'] );

Best Answer

It can be two issues:

  1. Use www.example.com/shop/index.php/api/v2_soap/?wsdl instead of www.example.com/shop/api/v2_soap?wsdl=1 (note the added index.php)
  2. Make sure that you disabled error_reporting/display_errors in your index.php.

Long note on the last point: The SoapClient isn't a browser and therefore doesn't send cookies, Magento in turn likes cookies and therefore Magento trows a Notice (when error_reporting is enabled), the notice makes your XML invalid/doesn't even make it load. (SOAP-ERROR: Parsing WSDL: Couldn't find <definitions>)

Related Topic