在 PHP 7.4 中生成 SOAP 数组

Generating SOAP Array in PHP 7.4

我一直在工作项目中使用 SOAP API(我真幸运!),WSDL 基本上毫无意义,因为请求的主体是 <any />,所以我必须生成SOAP 请求,而不是使用类映射选项。我正在使用这种方式 https://www.fischco.org/technica/2011/php-soap/ 生成一个对象数组,否则,正如 post 中所说,我得到了 <BOGUS> 标签。

这几乎是我多年来的做法,据我所知,这似乎也是很多其他人的做法 http://www.mm-newmedia.de/2017/10/php-und-bipro-sich-wiederholende-elemente-im-soap-request/ and http://andrecatita.com/code-snippets/php-soap-repeated-element-name/

一切正常,但我意识到我仍在使用 7.3 docker 图像,因此升级到 7.4,这不再有效,我只是得到数组的标签,而不是单个项目,我想这是因为 7.4 中的这个变化。

Calling get_object_vars() on an ArrayObject instance will now always return the properties of the ArrayObject itself (or a subclass). Previously it returned the values of the wrapped array/object unless the ArrayObject::STD_PROP_LIST flag was specified.

以前有人遇到过这个问题吗?我昨天整个下午都在想办法,但我完全被难住了

这是一个演示此行为的示例客户端

<?php

class Location
{
    public $Name;
}

// Overwrite the client so it doesn't actually make a request
class DebugSoapClient extends SoapClient
{
    public function __construct(string $wsdl, array $options = [])
    {
        $options = array_merge([
            'cache_wsdl'   => WSDL_CACHE_NONE,
            'exceptions'   => true,
            'features'     => SOAP_SINGLE_ELEMENT_ARRAYS,
            'soap_version' => SOAP_1_1,
            'trace'        => true,
        ], $options);

        parent::__construct($wsdl, $options);
    }

    public function __doRequest($request, $location, $action, $version, $one_way = 0): string
    {
        return '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><SubmitWorkItemResponse xmlns="http://example.com/webservices/DataServices/v2/ServicesAccess"><SubmitWorkItemResult></SubmitWorkItemResult></SubmitWorkItemResponse></soap:Body></soap:Envelope>';
    }
}

// Create an instance of the client
$namespace = 'http://example.com/webservices/DataServices/v2/ServicesAccess';
$client = new DebugSoapClient(__DIR__ . '/example.wsdl');
$client->__setSoapHeaders(
    new SoapHeader(
        $namespace,
        'AuthenticationHeader',
        ['Username' => 'Foo', 'Password' => 'Bar', 'ClientName' => 'Baz']
    )
);

// Build the body of the request
$locations = new ArrayObject();
foreach (['London', 'New York', 'Paris', 'Rome'] as $l) {
    $loc = new Location();
    $loc->Name = new SoapVar($l, XSD_STRING, '', '', 'Name', $namespace);

    $locations->append(new SoapVar($loc, SOAP_ENC_OBJECT, '', '', 'Location', $namespace));
}

$request = [
    'SourceData' => [
        'any' => new SoapVar($locations, SOAP_ENC_OBJECT, '', '', 'Locations', $namespace)
    ],
];

// Dispatch the request
$client->__soapCall('SubmitWorkItem', [$request]);

// Get the request body
$doc = new DomDocument('1.0');
$doc->loadXML($client->__getLastRequest());
$doc->formatOutput = true;

echo $doc->saveXML();

这是一个简化的 WSDL

<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://example.com/webservices/DataServices/v2/ServicesAccess" xmlns:s1="http://microsoft.com/wsdl/types/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" targetNamespace="http://example.com/webservices/DataServices/v2/ServicesAccess" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
    <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">Data Services</wsdl:documentation>
    <wsdl:types>
        <s:schema elementFormDefault="qualified" targetNamespace="http://example.com/webservices/DataServices/v2/ServicesAccess">
            <s:import namespace="http://microsoft.com/wsdl/types/" />
            <s:element name="SubmitWorkItem">
                <s:complexType>
                    <s:sequence>
                        <s:element minOccurs="1" maxOccurs="1" name="SourceData" type="tns:Source" />
                    </s:sequence>
                </s:complexType>
            </s:element>
             <s:complexType name="Source">
                <s:sequence>
                    <s:any />
                </s:sequence>
            </s:complexType>
            <s:element name="AuthenticationHeader" type="tns:AuthenticationHeader" />
            <s:complexType name="AuthenticationHeader">
                <s:sequence>
                    <s:element minOccurs="0" maxOccurs="1" name="Username" type="s:string" />
                    <s:element minOccurs="0" maxOccurs="1" name="Password" type="s:string" />
                    <s:element minOccurs="0" maxOccurs="1" name="ClientName" type="s:string" />
                </s:sequence>
                <s:anyAttribute />
            </s:complexType>
            <s:element name="SubmitWorkItemResponse">
                <s:complexType>
                    <s:sequence>
                        <s:element minOccurs="0" maxOccurs="1" name="SubmitWorkItemResult">
                            <s:complexType mixed="true">
                                <s:sequence>
                                    <s:any />
                                </s:sequence>
                            </s:complexType>
                        </s:element>
                    </s:sequence>
                </s:complexType>
            </s:element>
        </s:schema>
        <s:schema elementFormDefault="qualified" targetNamespace="http://microsoft.com/wsdl/types/">
            <s:simpleType name="guid">
                <s:restriction base="s:string">
                    <s:pattern value="[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}" />
                </s:restriction>
            </s:simpleType>
        </s:schema>
    </wsdl:types>
    <wsdl:message name="SubmitWorkItemSoapIn">
        <wsdl:part name="parameters" element="tns:SubmitWorkItem" />
    </wsdl:message>
    <wsdl:message name="SubmitWorkItemSoapOut">
        <wsdl:part name="parameters" element="tns:SubmitWorkItemResponse" />
    </wsdl:message>
    <wsdl:message name="SubmitWorkItemAuthenticationHeader">
        <wsdl:part name="AuthenticationHeader" element="tns:AuthenticationHeader" />
    </wsdl:message>
    <wsdl:portType name="ServicesAccessSoap">
        <wsdl:operation name="SubmitWorkItem">
            <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">This method accepts a single XML data block for processing.&lt;br&gt;Result returned synchronously.</wsdl:documentation>
            <wsdl:input message="tns:SubmitWorkItemSoapIn" />
            <wsdl:output message="tns:SubmitWorkItemSoapOut" />
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="ServicesAccessSoap" type="tns:ServicesAccessSoap">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
        <wsdl:operation name="SubmitWorkItem">
            <soap:operation soapAction="http://example.com/webservices/DataServices/v2/ServicesAccess/SubmitWorkItem" style="document" />
            <wsdl:input>
                <soap:body use="literal" />
                <soap:header message="tns:SubmitWorkItemAuthenticationHeader" part="AuthenticationHeader" use="literal" />
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal" />
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:binding name="ServicesAccessSoap12" type="tns:ServicesAccessSoap">
        <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
        <wsdl:operation name="SubmitWorkItem">
            <soap12:operation soapAction="http://example.com/webservices/DataServices/v2/ServicesAccess/SubmitWorkItem" style="document" />
            <wsdl:input>
                <soap12:body use="literal" />
                <soap12:header message="tns:SubmitWorkItemAuthenticationHeader" part="AuthenticationHeader" use="literal" />
            </wsdl:input>
            <wsdl:output>
                <soap12:body use="literal" />
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="ServicesAccess">
        <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">Data Services</wsdl:documentation>
        <wsdl:port name="ServicesAccessSoap" binding="tns:ServicesAccessSoap">
            <soap:address location="https://test.example.com/uat/ServicesAccess.asmx" />
        </wsdl:port>
        <wsdl:port name="ServicesAccessSoap12" binding="tns:ServicesAccessSoap12">
            <soap12:address location="https://test.example.com/uat/ServicesAccess.asmx" />
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

这是 7.3 及之前的输出

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://example.com/webservices/DataServices/v2/ServicesAccess">
  <SOAP-ENV:Header>
    <ns1:AuthenticationHeader>
      <ns1:Username>Foo</ns1:Username>
      <ns1:Password>Bar</ns1:Password>
      <ns1:ClientName>Baz</ns1:ClientName>
    </ns1:AuthenticationHeader>
  </SOAP-ENV:Header>
  <SOAP-ENV:Body>
    <ns1:SubmitWorkItem>
      <ns1:SourceData>
        <ns1:Locations>
          <ns1:Location>
            <ns1:Name>London</ns1:Name>
          </ns1:Location>
          <ns1:Location>
            <ns1:Name>New York</ns1:Name>
          </ns1:Location>
          <ns1:Location>
            <ns1:Name>Paris</ns1:Name>
          </ns1:Location>
          <ns1:Location>
            <ns1:Name>Rome</ns1:Name>
          </ns1:Location>
        </ns1:Locations>
      </ns1:SourceData>
    </ns1:SubmitWorkItem>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

这是 7.4 及更高版本的输出

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://example.com/webservices/DataServices/v2/ServicesAccess">
  <SOAP-ENV:Header>
    <ns1:AuthenticationHeader>
      <ns1:Username>Foo</ns1:Username>
      <ns1:Password>Bar</ns1:Password>
      <ns1:ClientName>Baz</ns1:ClientName>
    </ns1:AuthenticationHeader>
  </SOAP-ENV:Header>
  <SOAP-ENV:Body>
    <ns1:SubmitWorkItem>
      <ns1:SourceData>
        <ns1:Locations/>
      </ns1:SourceData>
    </ns1:SubmitWorkItem>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

您只需应用 1 个更改:

$request = [
    'SourceData' => [
        'any' => new SoapVar($locations->getArrayCopy(), SOAP_ENC_OBJECT, '', '', 'Locations', $namespace)
    ],
 ];
 // add this                           ^--------------^