donglao7947 2016-07-27 14:34
浏览 62
已采纳

SoapClient不会生成正确的请求

I have the SOAP endpoint and I would like to use PHP's \SoapClient class to send the request. The problem is that even if the "senderAddress" attribute has eg. "name", "nameDetail", "type" attributes (some of them are required by the docs), the generated XML request does not contain them. The only attribute it accepts/generates is the "id" attribute.

The same problem is also in another parts of the shipment object - f.ex. The Pickup part allows me to set just the "date" attribute, all of the others are skipped/ignored.

I've prepared some dummy code below, without data objects, just a simple array:

<?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();
}

Generated Request:

<?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>
  • 写回答

1条回答 默认 最新

  • doucang2871 2016-07-27 15:08
    关注

    The Shipment v1 schema states that the addressSender element is of type SHIPMENT:addressEnvelope, which is defined as such:

    <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>
    

    Since the addressEnvelope is a choice, you are only allowed to define one of the sequences. Since your data includes an id, the SoapClient uses the first sequence.

    Update Upon further testing, I can conclude that SoapClient will never choose the second sequence, since the first sequence only has an optional id element, which results in any data you provide being valid. The only way I was able to force SoapClient to choose the second sequence is by changing the minOccurs value of the id element to 1. To do this, you will have to download both the WSDL file and Shipment v1 schema, host them locally, and update the URLs.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数据量少可以用MK趋势分析吗
  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中