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

时间:2015-06-01 20:13:16

标签: php soap soap-client

尝试使用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元素都无济于事,并且最初省略了可选字段(在SoapUI界面中可以正常工作),但也没有任何乐趣。

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

1 个答案:

答案 0 :(得分:3)

你可能是正确的标题,或者至少是其中的一部分。我目前不在肥皂服务器的容易接触范围内,但我希望以下内容至少可以给出一些指示。

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

无论哪种方式,您都可以跳过__soapCall()方法并使用魔术方法,因为您似乎正在使用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...