【问题标题】:SOAP Communication between PHP client and windows service results in NullReferenceExceptionPHP 客户端和 Windows 服务之间的 SOAP 通信导致 NullReferenceException
【发布时间】:2013-10-01 09:07:24
【问题描述】:

如果我使用 SoapUi 测试服务,则操作失败,但如果使用 PHP 实现 Soap 客户端,则参数始终为空,我得到 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>

【问题讨论】:

    标签: c# php .net soap


    【解决方案1】:

    比较 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);
    

    【讨论】:

    • 嗨,谢谢我跟踪了两个 xml,soupui 一个可以工作,但是 php 客户端 xml 没有。但我没有看到错误。
    • 好的,如果我用 包装参数,它可以工作,但是如何实现呢?
    • @user2393454 查看编辑。我认为你必须有一个包装 Abonnent 对象。
    • $result = $client->Abonnieren(array('abonnent' => $abo));这个终于有用了,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    • 2012-12-16
    • 2012-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多