尝试在PHP中调用SOAP函数时出错

时间:2017-02-20 12:32:50

标签: php soap soap-client

当我试图用PHP调用SOAP函数时,我已经坚持了几个星期。

错误:

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

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>

我正在使用此代码来调用函数:

$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);
    }

如果您想了解更多信息,请问我。我是这个社区的新手。

1 个答案:

答案 0 :(得分:0)

我解决了它,我必须将SoapHeader放在stdClass对象中,并将其转换为数组。我不知道它为什么会起作用,但确实如此。