Search code examples
phpsoapsoap-client

SOAP request with soapenv


I have the following code for connecting to a soap client. I am struggling hard to find how to create a proper soap request.

$soap = new SoapClient($apiWsdl,
        array("soap_version" => SOAP_1_1,
              "trace" => 1));
var_dump($soap);
echo $soap->__getLastResponse();
try{
    $data = $soap->login($apiUser,$apiKey);
}
catch (SoapFault $soapFault) {
    echo "Request :<br>", htmlentities($soap->__getLastRequest()), "<br>";
    echo "Response :<br>", htmlentities($soap->__getLastResponse()), "<br>";

}

I get the following XML built:

<?xml version="1.0" encoding="UTF-8"?> \<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:Magento" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:login><username xsi:type="xsd:string">XXXXXXX</username><apiKey xsi:type="xsd:string">XXXXXXXX</apiKey></ns1:login></SOAP-ENV:Body></SOAP-ENV:Envelope> 


Which returns be a forbidden access.

However the server is accepting only request of the following form



<soapenv:Envelope 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:urn="urn:Magento">
   <soapenv:Header/>
   <soapenv:Body>
      <urn:login soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <username xsi:type="xsd:string">XXXXXXX</username>
         <apiKey xsi:type="xsd:string">XXXXXX</apiKey>
      </urn:login>
   </soapenv:Body>
</soapenv:Envelope>

Can somebody please let me know what mistake I am making in the code.


Solution

  • It looks like you have a typo right after the XML tags. There is a backslash that should not be there. This could trigger your errors.

    HTH, Jim