SOAP调用在SoapUI中工作,但在PHP中使用soapclient - Object引用问题失败


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

尝试使用PHP 5.x查询托管在IIS服务器上的.NET web服务

$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()输出时,它似乎显示了包装器,但没有显示查询元素

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元素和不带Token元素,但都无济于事,并且最初省略了可选字段(在SoapUI接口中工作得很好),但也没有什么乐趣。

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

关于标题是原因,或者至少是部分原因,你可能是对的。我目前还没有找到soap服务器,但我希望下面的文章至少能给一些指点。

这里有两种可能性:头应该作为SoapHeader对象包含,或者您需要以不同的方式构建参数数组。我将列出两个版本。

无论哪种方式,您都可以跳过__soapCall()方法而使用magic方法,因为您似乎正在使用wsdl.

参数版本(先试试这个)

如果幸运的话,您只需要重新格式化正文以适应给定的模式。它看起来就像这样。像这样:

$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版本

如果你真的需要一个合适的头,你需要做的是创建一个头对象,并将它附加到你实例化的客户端。然后,您可以调用端点。

要做到这一点,您需要知道名称空间,在您的模式中应该称为targetNameSpace。您还需要知道名称,您可以在例如SoapUI中看到。

最后,直接提供参数就足够了——不需要将它们放在单元素数组中。最后得到如下图所示的结果。幸运的话,这至少能让你找到正确的方向。:)

// 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...