PHP 客户端和 Windows 服务之间的 SOAP 通信会导致 NullReferenceException

本文关键字:通信 NullReferenceException SOAP 客户端 Windows 服务 之间 PHP | 更新日期: 2023-09-27 18:33:29

如果我用 SoapUi 测试服务,操作就会失败,但如果用 PHP 实现 Soap 客户端,参数都是 emty 并且我得到一个 NullReferenceException。

<?php
$options = array();
$options['classmap']['Abonnent']  = 'RequestType';
$client = new SoapClient('wsdllink',$options);
 class RequestType
{
   public $Email;
   public $Nachname;
   public $Passwort;
   public $Vorname;
}
$abo         = new RequestType;
$abo->Email    = 'email';
$abo->Nachname    = 'lastname';
$abo->Passwort    = 'passwort';
$abo->Vorname    = 'firstname';
try {
    $result = $client->Abonnieren($abo);
} catch(SoapFault $e) {
    echo "Request :'n". ($client->__getLastRequest()). "'n";
    echo "Response :'n". ($client->__getLastResponseHeaders()). "'n";
    echo "Response :'n". ($client->__getLastResponse()). "'n";
    echo($e->getMessage());
} catch(Exception $e) {
    echo $e->getMessage();
}
?>

var_dump($client->__getFunctions())

 array(2) {
  [0]=>
  string(53) "AbonnierenResponse Abonnieren(Abonnieren $parameters)"
  [1]=>
  string(53) "RegisterIBResponse RegisterIB(RegisterIB $parameters)"
}

var_dump($client->__getTypes())

 array(8) {
  [0]=>
  string(87) "struct Abonnent {
 string Email;
 string Nachname;
 string Passwort;
 string Vorname;
}"
  [1]=>
  string(41) "struct Abonnieren {
 Abonnent abonnent;
}"
  [2]=>
  string(56) "struct AbonnierenResponse {
 boolean AbonnierenResult;
}"
  [3]=>
  string(41) "struct RegisterIB {
 Abonnent abonnent;
}"
  [4]=>
  string(56) "struct RegisterIBResponse {
 boolean RegisterIBResult;
}"
  [5]=>
  string(8) "int char"
  [6]=>
  string(17) "duration duration"
  [7]=>
  string(11) "string guid"
}

来自 SoupUI 的 XML

<soapenv:Envelope
 xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:tem="http://tempuri.org/"
  xmlns:prm="http://schemas.datacontract.org/2004/07/PRMarketService">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:Abonnieren>
         <tem:abonnent>
            <prm:Email>email</prm:Email>
            <prm:Nachname>lastname</prm:Nachname>
            <prm:Passwort>passwort</prm:Passwort>
            <prm:Vorname>firstname</prm:Vorname>
         </tem:abonnent>
      </tem:Abonnieren>
   </soapenv:Body>
</soapenv:Envelope>

来自 PHP 客户端的 XML

 <?xml version="1.0" encoding="UTF-8"?>
   <SOAP-ENV:Envelope
        xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
        xmlns:ns1="http://schemas.datacontract.org/2004/07/PRMarketService"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:ns2="http://tempuri.org/">
            <SOAP-ENV:Body>
                <ns2:Abonnieren xsi:type="ns1:Abonnent">
                    <ns1:Email>email</ns1:Email>
                    <ns1:Nachname>lastname</ns1:Nachname>
                    <ns1:Passwort>passwort</ns1:Passwort>
                    <ns1:Vorname>firstname </ns1:Vorname>
                </ns2:Abonnieren>
            </SOAP-ENV:Body>
        </SOAP-ENV:Envelope>

PHP 客户端和 Windows 服务之间的 SOAP 通信会导致 NullReferenceException

比较 SoapUI 和 PHP 的 SoapClient 发送的消息,例如使用 Fiddler。如果我没记错的话,你必须将 PHP 发送的 XML 主体包装在一个以适当的 SOAPAction 命名的根元素中。

像这样:

class AbonnierenRequest
{
    public $Abbonent;   
}
class Abonnent
{
    public $Email;
    public $Nachname;
    public $Passwort;
    public $Vorname;
}
$request = new AbonnierenRequest();
$request->Abonnent = new Abonnent();
$request->Abonnent->Email = 'email';
$request->Abonnent->Nachname = 'lastname';
$request->Abonnent->Passwort = 'passwort';
$request->Abonnent->Vorname = 'firstname';
$result = $client->Abonnieren($request);