【问题标题】:Not getting the required SOAP request XML未获得所需的 SOAP 请求 XML
【发布时间】:2016-07-11 03:42:41
【问题描述】:

我正在开发一个使用 OCPP(开放充电点协议)的简单 php 客户端。我已经创建了客户端,这是来自我的代码的请求 XML。

<?xml version="1.0" encoding="UTF-8"?>
  <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="urn://Ocpp/Cs/2015/10/">
      <env:Header>
          <ns1:chargeBoxIdentity env:mustUnderstand="true">XXX01</ns1:chargeBoxIdentity>
          <ns1:Action env:mustUnderstand="true">/Authorize</ns1:Action>
          <ns1:MessageId env:mustUnderstand="true">123</ns1:MessageId>
          <ns1:RelatesTo>relatesTo</ns1:RelatesTo>
          <ns1:From/>
          <ns1:ReplyTo/>
          <ns1:To/>
      </env:Header>
      <env:Body>
          <ns1:authorizeRequest>
          <ns1:idTag>1234567</ns1:idTag>
          </ns1:authorizeRequest>
      </env:Body>
  </env:Envelope>

但我想得到这个 XML 输出

<?xml version='1.0' encoding='utf8'?> 
<ns0:Envelope xmlns:ns0="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="urn://Ocpp/Cs/2015/10/">
    <ns0:Header>
        <ns1:chargeBoxIdentity mustUnderstand="true">XXX01 </ns1:chargeBoxIdentity>
        <ns1:Action mustUnderstand="true">/Authorize</ns1:Action>
        <ns1:MessageId mustUnderstand="true">123</ns1:MessageId>
        <ns1:RelatesTo>relatesTo</ns1:RelatesTo>
        <ns1:From />
        <ns1:ReplyTo />
        <ns1:To />
    </ns0:Header>
    <ns0:Body>
        <ns1:IdTag>1234567</ns1:IdTag>
    </ns0:Body>
</ns0:Envelope>

请注意,我的代码有env:Envelope,输出有ns0:Envelope,并且在我的代码中,Soap Body 中有额外的属性。我对 php SOAP 的了解非常有限。相关代码如下。

ini_set('soap.wsdl_cache_enabled',0);

$wsdl_centralsystem = "OCPP_centralsystemservice_1.5_final.wsdl";
$params =  "0.1.1.255.0.0.1.0.";

$vLocation = "linktoserver/server.php";

$client = new SoapClient($wsdl_centralsystem, array(

    "soap_version" => SOAP_1_2,
    "location" => $vLocation,
    "trace"=>1, "exceptions"=>0,

));

$chargeboxid = "XXX01";
$action = "/Authorize";
$msgid = "123";
$relatesto = "relatesTo";


//Create Soap Headers
$headerCchargeBoxIdentity = new SoapHeader("urn://Ocpp/Cs/2015/10/", 'chargeBoxIdentity', $chargeboxid, true);
$headerAction = new SoapHeader("urn://Ocpp/Cs/2015/10/", 'Action', $action, true);
$headerMessageId = new SoapHeader("urn://Ocpp/Cs/2015/10/", 'MessageId', $msgid, true);
$headerRelatesTo = new SoapHeader("urn://Ocpp/Cs/2015/10/", 'RelatesTo', $relatesto, false);
$headerFrom = new SoapHeader("urn://Ocpp/Cs/2015/10/", 'From', NULL, false);
$headerReplyTo= new SoapHeader("urn://Ocpp/Cs/2015/10/", 'ReplyTo', NULL, false);
$headerTo = new SoapHeader("urn://Ocpp/Cs/2015/10/", 'To', NULL, false);

//set the Headers of Soap Client.
$client->__setSoapHeaders(array($headerCchargeBoxIdentity,$headerAction,$headerMessageId,$headerRelatesTo,$headerFrom,$headerReplyTo,$headerTo));


$output = $client-> __soapCall('Authorize',array($params));

【问题讨论】:

    标签: php xml soap wsdl client


    【解决方案1】:

    如果前缀与Envelope 元素上的xmlns 声明匹配,那么它是有效的XML,因此是有效的SOAP,所以你应该没问题。但是,XML 区分大小写,我注意到您的代码在 Body 元素中包含和 idTag 元素,而不是您期望的输出中的 IdTag 元素。

    【讨论】:

      【解决方案2】:

      根据 OCPP 规范,您当前的输出更接近正确,但仍有很多问题。

      • 您使用的完整 OCPP URN 以 .../2015/10/ 结尾,该 .../2015/10/ 用于 OCPP 1.6,但在您的代码中,您将 WSDL 用于 OCPP 1.5,这需要 .../2012/06/ 用于 URN。
      • 根据 OCPP 规范,驼峰式 idTagAuthorize 消息的正确字段名称。
      • 您需要 SOAP 正文中的根标记 &lt;authorizeRequest /&gt;
      • 您需要 WSA(寻址)命名空间 xmlns:wsa="http://www.w3.org/2005/08/addressing" 用于 SOAP 标头中的寻址字段(MessageIdFromToReplyToRelatesToAction
      • chargeBoxIdentity 属于 OCPP URN 命名空间。
      • MessageId 应该是 MessageID 并且它不应该有 mustUnderstand 属性。
      • ActionToReplyTochargeBoxIdentity 应该具有 mustUnderstand="true" 属性。其他人不应该。
      • 从 OCPP 1.6 开始,ReplyTo 是必需的,并且应该始终具有值 http://www.w3.org/2005/08/addressing/anonymous
      • RelatesTo 仅用于回复。这是一个请求。

      预期输出的最终版本应如下所示:

      <?xml version="1.0" encoding="UTF-8"?>
      <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" 
              xmlns:cs="urn://Ocpp/Cs/2012/06/"
              xmlns:wsa="http://www.w3.org/2005/08/addressing">
          <soap:Header>
              <cs:chargeBoxIdentity soap:mustUnderstand="true">XXX01</cs:chargeBoxIdentity>
              <wsa:Action soap:mustUnderstand="true">/Authorize</wsa:Action>
              <wsa:MessageID>123</wsa:MessageID>
              <wsa:From><wsa:Address>http://from-endpoint</wsa:Address></wsa:From>
              <wsa:ReplyTo soap:mustUnderstand="true"><wsa:Address>http://www.w3.org/2005/08/addressing/anonymous</wsa:Address></wsa:ReplyTo>
              <wsa:To soap:mustUnderstand="true"><wsa:Address>http://to-endpoint</wsa:Address></wsa:To>
          </soap:Header>
          <soap:Body>
              <cs:authorizeRequest>
                  <cs:idTag>1234567</cs:idTag>
              </cs:authorizeRequest>
          </soap:Body>
      </soap:Envelope>
      

      注意:我已经设置了一些有意义的命名空间前缀,你可以设置任何你喜欢的东西。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-26
        • 2017-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多