繁体   English   中英

在 PHP 7.4 中生成 SOAP 数组

[英]Generating SOAP Array in PHP 7.4

I have been using a SOAP API in a work project (yay lucky me,), the WSDL is basically pointless as the body of the request is <any /> so I am having to generate the SOAP request rather than using the classmap option. 我正在使用这种方式https://www.fischco.org/technica/2011/php-soap/来生成对象数组,否则,正如帖子中所说,我得到了<BOGUS>标签。

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

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

在 ArrayObject 实例上调用get_object_vars()现在将始终返回 ArrayObject 本身(或子类)的属性。 以前它返回包装数组/对象的值,除非指定了ArrayObject::STD_PROP_LIST标志。

以前有人遇到过这个问题吗? 我昨天整个下午都在试图弄清楚,但我完全被难住了

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

<?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 及之前版本中的 output

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

这是 output 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                           ^--------------^

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM