我正在开发一个使用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 正文中。我对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));
如果前缀与Envelope
元素上的xmlns
声明匹配,那么它是有效的 XML,因此是有效的 SOAP,所以你应该没问题。但是,XML 区分大小写,我注意到您的代码在 Body
元素内部具有并idTag
元素,而不是您期望的输出中的IdTag
元素。
根据 OCPP 规范,您当前的输出更接近正确的输出,但仍存在许多问题。
- 您正在使用的完整 OCPP URN 以
.../2015/10/
结尾,这适用于 OCPP 1.6,但在您的代码中,您使用的是 OCPP 1.5 的 WSDL,这需要 URN 的.../2012/06/
。 - 根据 OCPP 规范,驼峰大小写
idTag
是Authorize
消息的正确字段名称。 - 您需要在 SOAP 正文中
<authorizeRequest />
根标记。 - 您需要 WSA(寻址(命名空间
xmlns:wsa="http://www.w3.org/2005/08/addressing"
来寻址 SOAP 标头中的字段(MessageId
、From
、To
、ReplyTo
、RelatesTo
和Action
。 -
chargeBoxIdentity
属于 OCPP URN 命名空间。 -
MessageId
应该是MessageID
的,它不应该mustUnderstand
属性。 -
Action
、To
、ReplyTo
和chargeBoxIdentity
应该有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>
注意:我已经设置了一些有意义的命名空间前缀,您可以设置任何您喜欢的内容。