Search code examples
phpsoapsoap-client

Error when trying to call a SOAP function in PHP


I'm stuck for a couple of weeks now when trying to call a SOAP function in PHP.

The error:

Server was unable to process request. ---> Object reference not set to an instance of an object

The XML:

POST /example.com/example.asmx HTTP/1.1
Host: URL
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.example.com/action"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <AuthenticationHeader xmlns="http://www.example.com/">
      <username>string</username>
      <password>string</password>
      <Id>long</Id>
    </AuthenticationHeader>
  </soap:Header>
  <soap:Body>
    <GetCardInfo xmlns="http://www.example.com/">
      <cardnumber>string</cardnumber>
      <pincode>string</pincode>
    </GetCardInfo>
  </soap:Body>
</soap:Envelope>

I'm using this code to call the function:

$client = new SoapClient("http://example.com/example.asmx?WSDL", array('trace' => 1));    

$card = array(
      'cardnumber' => 'example card number',
      'pincode' => 'example pin code'
    );

    try {
      $result = $client->__soapCall('GetCardInfo', $card);
      print_r($result);
    }

    catch (SoapFault $q)
    {
      print_R($q);
    }

If you would like more info, please ask me. I'm new to this community.


Solution

  • I solved it, I had to put the SoapHeader in the stdClass object, and cast it to an array. I have no idea why it worked but it does.