PHP SoapClient不处理WSDL中的abstract和substitutionGroup属性

bil*_*oah 14 php soap wsdl soap-client

我正在使用他们提供的wsdl向Canada Post发送一个电话,其中包含以下部分:

<!-- group-id and transmit-shipment are mutually exclusive -->
<xsd:element name="groupIdOrTransmitShipment" abstract="true" />
<xsd:element name="group-id" type="tns:GroupIDType" substitutionGroup="tns:groupIdOrTransmitShipment"/>
<xsd:element name="transmit-shipment" type="xsd:boolean" fixed="true" substitutionGroup="tns:groupIdOrTransmitShipment"/>

<xsd:complexType name="ShipmentType">
    <xsd:all>
        <xsd:element ref="tns:groupIdOrTransmitShipment" />
        <xsd:element name="quickship-label-requested" type="xsd:boolean" minOccurs="0"/>
        <xsd:element name="cpc-pickup-indicator" type="xsd:boolean" fixed="true" minOccurs="0"/>
        <xsd:element name="requested-shipping-point" type="tns:PostalCodeType" minOccurs="0"/>
        <xsd:element name="shipping-point-id" type="tns:OutletIDType" minOccurs="0" />
        <xsd:element name="expected-mailing-date" type="xsd:date" minOccurs="0"/>
        <xsd:element name="delivery-spec" type="tns:DeliverySpecType"/>
        <xsd:element name="return-spec" type="tns:ReturnSpecType" minOccurs="0"/>
    </xsd:all>
</xsd:complexType>
Run Code Online (Sandbox Code Playgroud)

在他们的代码示例中,他们扩展了Soap类并覆盖了__doRequest()Soap方法,如下所示:

/*
 Need to override SoapClient because the abstract element 'groupIdOrTransmitShipment' is expected to be in the request in order for validation to pass.
So, we give it what it expects, but in __doRequest we modify the request by removing the abstract element and add the correct element.

*/

class MySoapClient extends SoapClient {

    function __construct($wsdl, $options = null) {
        parent::__construct($wsdl, $options);
    }

    function __doRequest($request, $location, $action, $version, $one_way = NULL) {
        $dom = new DOMDocument('1.0');
        $dom->loadXML($request);
        //get element name and values of group-id or transmit-shipment.
        $groupIdOrTransmitShipment =  $dom->getElementsByTagName("groupIdOrTransmitShipment")->item(0);
        $element = $groupIdOrTransmitShipment->firstChild->firstChild->nodeValue;
        $value = $groupIdOrTransmitShipment->firstChild->firstChild->nextSibling->firstChild->nodeValue;

        //remove bad element
        $newDom = $groupIdOrTransmitShipment->parentNode->removeChild($groupIdOrTransmitShipment);

        //append correct element with namespace
        $body =  $dom->getElementsByTagName("shipment")->item(0);
        $newElement = $dom->createElement($element, $value);
        $body->appendChild($newElement);

        //save $dom to string
        $request = $dom->saveXML();
        //echo $request;

        //doRequest
        return parent::__doRequest($request, $location, $action, $version);
    }
}
Run Code Online (Sandbox Code Playgroud)

使用他们的overover方法可以正常工作,因为它改变了xml请求,但是为什么他们这样做会让我感到非常困惑.WSDL是否存在根本性的错误?这是PHP的SoapClient中的错误吗?我想要一种发送有效请求而不覆盖任何核心类方法的方法.

根据我的理解,作者想要一个group-id 一个transmit-shipment元素,但不是两个或没有.由于你不能在<choice>元素中有一个<all>元素,根据w3c.org上的讨论,为此目的声明一个替换组应该没问题,并且在我的请求中只包含两个元素中的一个,但如果我只包括transmit-shipment并省略抽象元素groupIdOrTransmitShipmentphp返回错误:

SOAP-ERROR: Encoding: object has no 'groupIdOrTransmitShipment' property
Run Code Online (Sandbox Code Playgroud)

更新: 这是非常古老但可能相关吗?https://bugs.php.net/bug.php?id=48570

小智 1

我还必须覆盖和扩展 php SoapClass。它与 php肥皂类期望请求/响应中的特定格式然后接收/发送的内容有关。扩展它使我能够自己处理格式。

wsdl 没有任何问题,错误是类的一部分。没有办法绕过覆盖