SoapClient未生成正确的请求



我有SOAP端点,我想使用PHP的\SoapClient类来发送请求。问题是,即使"senderAddress"属性具有例如"name"、"nameDetail"、"type"属性(其中一些属性是文档所必需的),生成的XML请求也不包含这些属性。它接受/生成的唯一属性是"id"属性。

同样的问题也出现在发货对象的另一个部分——例如。提货部分允许我只设置"日期"属性,所有其他部分都被跳过/忽略。

我在下面准备了一些伪代码,没有数据对象,只有一个简单的数组:

<?php
$soapClient = new SoapClient("https://capi.dpdportal.sk/apix/shipment/?wsdl", [
    'trace' => 1
]);
$headers = array();
$dpdSecurity = new stdClass();
$token = new stdClass();
$token->ClientKey = "topsecretkey";
$token->Email = "topsecretmail";
$dpdSecurity->SecurityToken = $token;
$headers["auth"] = new SoapHeader('http://www.dpdportal.sk/XMLSchema/DPDSecurity/v2', 'DPDSecurity', $dpdSecurity);
$soapClient->__setSoapHeaders($headers);
$shipment = [
    "reference" => "123",
    "delisId" => "123",
    "addressSender" => [
        "type" => "b2c", // this attribute is missing in the Request
        "id" => 41656415651,
        "nameDetail" => "test", // this attribute is missing in the Request
    ],
    "addressRecipient" => "123",
    "product" => 9,
    "parcels" => [],
    "pickup" => null,
];
$params = [
    'shipment' => $shipment,
];
try {
    $response = $soapClient->CreateV1($params);
    echo '==' . PHP_EOL;
    var_dump($response);
} catch (Exception $e) {
    echo $e->getMessage();
}

生成的请求:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.dpdportal.sk/XMLSchema/SHIPMENT/v1" xmlns:ns2="http://www.dpdportal.sk/XMLSchema/DPDSecurity/v2">
   <SOAP-ENV:Header>
      <ns2:DPDSecurity>
         <ns2:SecurityToken>
            <ns2:ClientKey>topsecretkey</ns2:ClientKey>
            <ns2:Email>topsecretmail</ns2:Email>
         </ns2:SecurityToken>
      </ns2:DPDSecurity>
   </SOAP-ENV:Header>
   <SOAP-ENV:Body>
      <ns1:CreateRequest>
         <ns1:shipment>
            <ns1:reference>123</ns1:reference>
            <ns1:delisId>123</ns1:delisId>
            <ns1:product>9</ns1:product>
            <ns1:pickup />
            <ns1:addressSender>
               <ns1:id>41656415651</ns1:id>
            </ns1:addressSender>
            <ns1:addressRecipient />
            <ns1:parcels />
         </ns1:shipment>
      </ns1:CreateRequest>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Shipment v1模式表示addressSender元素的类型为SHIPMENT:addressEnvelope,其定义如下:

<xsd:complexType name="addressEnvelope">
    <xsd:annotation>
        <xsd:documentation>Address envelope</xsd:documentation>
    </xsd:annotation>
    <xsd:choice>
        <xsd:sequence>
            <xsd:element name="id" type="SHIPMENT:idType" minOccurs="0">
                <xsd:annotation>
                    <xsd:documentation>Address ID</xsd:documentation>
                </xsd:annotation>
            </xsd:element>
        </xsd:sequence>
        <xsd:sequence>
            <xsd:element name="type" type="SHIPMENT:addresstypeType" minOccurs="0" />
            <xsd:element name="name" type="SHIPMENT:nameType" minOccurs="0">
                <xsd:annotation>
                    <xsd:documentation>Contact person</xsd:documentation>
                </xsd:annotation>
            </xsd:element>
            <xsd:element name="nameDetail" type="SHIPMENT:nameType" minOccurs="0">
                <xsd:annotation>
                    <xsd:documentation>Contact person (detail)</xsd:documentation>
                </xsd:annotation>
            </xsd:element>
            <xsd:element name="street" type="SHIPMENT:streetType" minOccurs="0" />
            <xsd:element name="streetDetail" type="SHIPMENT:streetType" minOccurs="0">
                <xsd:annotation>
                    <xsd:documentation>Street (detail)</xsd:documentation>
                </xsd:annotation>
            </xsd:element>
            <xsd:element name="houseNumber" type="SHIPMENT:houseNumberType" minOccurs="0" />
            <xsd:element name="zip" type="SHIPMENT:zipType" minOccurs="0" />
            <xsd:element name="country" type="SHIPMENT:countryType" minOccurs="0" />
            <xsd:element name="city" type="SHIPMENT:cityType" minOccurs="0" />
            <xsd:element name="phone" type="SHIPMENT:phoneType" minOccurs="0" />
            <xsd:element name="email" type="SHIPMENT:emailType" minOccurs="0" />
            <xsd:element name="reference" type="SHIPMENT:referenceType" minOccurs="0">
                <xsd:annotation>
                    <xsd:documentation>Reference for address (e.g. specific code of client)</xsd:documentation>
                </xsd:annotation>
            </xsd:element>
            <xsd:element name="note" type="SHIPMENT:noteType" minOccurs="0">
                <xsd:annotation>
                    <xsd:documentation>Free note related to address</xsd:documentation>
                </xsd:annotation>
            </xsd:element>
            <xsd:element name="ico" type="SHIPMENT:icoType" minOccurs="0" />
            <xsd:element name="vatId" type="SHIPMENT:vatIdType" minOccurs="0" />
            <xsd:element name="vatId2" type="SHIPMENT:vatId2Type" minOccurs="0" />
        </xsd:sequence>
    </xsd:choice>
</xsd:complexType>

由于addressEnvelope是一个选项,因此您只能定义其中一个序列。由于您的数据包括一个id,SoapClient使用第一个序列。

更新经过进一步的测试,我可以得出结论,SoapClient永远不会选择第二个序列,因为第一个序列只有一个可选的id元素,这会导致您提供的任何数据都是有效的。我能够迫使SoapClient选择第二个序列的唯一方法是将id元素的minOccurs值更改为1。要做到这一点,您必须下载WSDL文件和Shipment v1模式,在本地托管它们,并更新URL。

相关内容

  • 没有找到相关文章

最新更新