SOAP 调用在 SoapUI 中有效,但在 PHP 使用 soapclient 时失败 - Object 参考问题

SOAP call works in SoapUI but fails in PHP using soapclient - Object reference issue

正在尝试使用 PHP 5.x

查询托管在 IIS 服务器上的 .NET 网络服务
$wsdl_path = "http://192.168.1.1/TestSite/TestService.asmx?wsdl";
$soapClient = new SoapClient($wsdl_path, array('trace' => 1));
$dump=var_export($soapClient->__getFunctions(), true);
echo htmlentities($dump);

产生

array (
  0 => 'ProcessTransactionResponse ProcessTransaction(ProcessTransaction $parameters)',
  1 => 'ProcessTransactionResponse ProcessTransaction(ProcessTransaction $parameters)',
)

这表明它正在正确访问 wsdl 文件。

使用 SoapUI 验证的格式正确的查询如下

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.mydomain.tld/">
   <soapenv:Header/>
   <soapenv:Body>
      <ws:ProcessTransaction>
        <!--Optional:-->
         <ws:request>
            <!--Optional:-->
            <ws:Header>
               <!--Optional:-->
               <ws:Token>hello</ws:Token>
            </ws:Header>
            <!--Optional:-->
            <ws:Parameters>
               <ws:DeviceID>12345</ws:DeviceID>
               <ws:SourceScreen>12345</ws:SourceScreen>
               <!--Optional:-->
               <ws:Language>E</ws:Language>
               <ws:LocalDateTime>2015-05-20T11:59:29.910Z</ws:LocalDateTime>
               <ws:TicketID>12345</ws:TicketID>
               <ws:PayScreenAttributeID>12345</ws:PayScreenAttributeID>
               <!--Optional:-->
               <ws:InputValue>1234556789</ws:InputValue>
               <ws:PaymentAmount>0</ws:PaymentAmount>
               <!--Optional:-->
               <ws:POSReceiptCustomer>?</ws:POSReceiptCustomer>
               <!--Optional:-->
               <ws:POSReceiptMerchant>?</ws:POSReceiptMerchant>
            </ws:Parameters>
         </ws:request>
      </ws:ProcessTransaction>
   </soapenv:Body>
</soapenv:Envelope>

所以为了用 PHP 和 SoapClient 复制它,我将数据元素收集在一个数组中

$inputParams=array(
    'Token' => 'hello',
    'DeviceID' => 12345,
    'SourceScreen' => 12345,
    'Language' => 'E',
    'LocalDateTime' => '2015-05-20T11:59:29.910Z',  
    'TicketID' => 12345,
    'PayScreenAttributeID' => 12345,
    'InputValue' => '123456789',
    'PaymentAmount' => 0,
    'POSReceiptCustomer' => '?',
    'POSReceiptMerchant' => '?',
);

并执行查询

try {
    $response = $soapClient->__soapCall('ProcessTransaction', array('parameters' => $inputParams));
    var_dump($response);
} catch (SoapFault $fault) {
    var_dump($fault);
    echo "SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})";
    echo "REQUEST:\n" . htmlentities($soapClient->__getLastRequest()) . "\n";
}

我得到了可怕的 Server was unable to process request. ---> Object reference not set to an instance of an object. 这不是很有用。

当我查看 __getLastRequest() 输出时,它似乎显示了包装器,但 none 查询元素

REQUEST:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://ws.mydomain.tld/"><SOAP-ENV:Body><ns1:ProcessTransaction/></SOAP-ENV:Body></SOAP-ENV:Envelope>

我试过使用和不使用 Token 元素都无济于事,并且最初省略了可选字段(在 SoapUI 界面中工作正常)但也没有任何乐趣。

我怀疑它与带有 Token 元素的附加 header 容器有关,因为我们的其他 soap 实现没有包含此元素。

您可能是对的 header 是原因,或者至少是部分原因。我目前无法轻松访问 soap 服务器,但我希望下面的内容至少可以提供一些指示。

这里有两种可能性:要么 header 应该作为 SoapHeader object 包含,要么您需要以不同的方式构建参数数组。我会列出两个版本。

无论哪种方式,您都可以跳过 __soapCall() 方法并改用魔术方法,因为您似乎在使用 wsdl。

参数版本(先试试这个)

如果幸运的话,您只需重新格式化 body 即可适应给定的架构。老实说,它看起来像那样。像这样:

$params = array(
    'request' => array(
        'Header' => array(
            'Token' => 'hello'
        ),
        'Parameters' => array(
            'DeviceID' => 12345,
            'SourceScreen' => 12345,
            'Language' => 'E',
            'LocalDateTime' => '2015-05-20T11:59:29.910Z',  
            'TicketID' => 12345,
            'PayScreenAttributeID' => 12345,
            'InputValue' => '123456789',
            'PaymentAmount' => 0,
            'POSReceiptCustomer' => '?',
            'POSReceiptMerchant' => '?'
        )
    )
);

$wsdl_path = "http://192.168.1.1/TestSite/TestService.asmx?wsdl";
$soapClient = new SoapClient($wsdl_path, array('trace' => 1));
$response = $soapClient->ProcessTransaction($params);

SoapHeader 版本

如果您确实需要一个合适的 header,您需要做的是创建一个 header object 并将其附加到您实例化的客户端。然后,您可以调用端点。

为此,您需要知道命名空间,它在您的架构中应称为 targetNameSpace。您还需要知道名称,您可以在例如SoapUI.

最后,直接提供参数应该就足够了——不需要将它们放在 single-element 数组中。所以你最终得到类似下面的东西。运气好的话,这至少会让你走上正确的方向。 :)

// instantiate soap client
$wsdl_path = "http://192.168.1.1/TestSite/TestService.asmx?wsdl";
$soapClient = new SoapClient($wsdl_path, array('trace' => 1));

// create and attach a header
$header = new SoapHeader($namespace, $name, array('Token' => 'hello'));
$soapClient->__setSoapHeaders($header);

// call the endpoint
$response = $soapClient->ProcessTransaction($inputParams);
var_dump($response); // hopefully you get something back...