1

我正在测试一个必须在一个请求中接收多个操作的 SOAP 服务器。服务器配置了 Symfony 2.7、PHP 7.1 和 zend-soap 2.7。我现在无法升级 Symfony 和 PHP 的版本,除非它们是导致此问题的原因。

我正在尝试一个非常虚拟的代码。控制器如下:

if ($request->isMethod('GET') and isset($_GET['wsdl'])) {
    $response = new Response();
    $response->headers->set('Content-Type', 'text/xml');

    $wsdl = fopen('dummy.wsdl', 'r');

    $response->setContent(fread($wsdl, filesize('dummy.wsdl')));

    return $response;
}
elseif ($request->isMethod('POST')) {
    $server = new Server('dummy.wsdl');
    $server->setObject($this->get('app.service.soap_dummy'));

    $response = new Response();
    $response->headers->set('Content-Type', 'text/xml; charset=ISO-8859-1');

    ob_start();
    $server->handle();
    $response->setContent(ob_get_clean());

    return $response;
}

它基本上调用一个具有单一功能的服务:

class SoapDummy
{
/**
 * @return int
 */
public function getNumber()
{
    return 1;
}

}

WSDL 文件是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://localhost/soap/dummy" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" name="SoapDummy" targetNamespace="http://localhost/soap/dummy">
<types>
    <xsd:schema targetNamespace="http://localhost/soap/dummy"/>
</types>
<portType name="SoapDummyPort">
    <operation name="getNumber">
        <documentation>getNumber</documentation>
        <input message="tns:getNumberIn"/>
        <output message="tns:getNumberOut"/>
    </operation>
</portType>
<binding name="SoapDummyBinding" type="tns:SoapDummyPort">
    <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="getNumber">
        <soap:operation soapAction="http://localhost/soap/dummy#getNumber"/>
        <input><soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost/soap/dummy"/></input>
        <output><soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost/soap/dummy"/></output>
    </operation>
</binding>
<service name="SoapDummyService">
    <port name="SoapDummyPort" binding="tns:SoapDummyBinding">
        <soap:address location="http://localhost/soap/dummy"/>
    </port>
</service>
<message name="getNumberIn"/>
<message name="getNumberOut">
    <part name="return" type="xsd:int"/>
</message>

我做下一个请求:

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<Body>
    <getNumber xmlns="http://localhost/soap/producto"/>
    <getNumber xmlns="http://localhost/soap/producto"/>
</Body>
</Envelope>

我得到下一个回应:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://localhost/soap/dummy" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <SOAP-ENV:Body>
        <ns1:getNumberResponse>
            <return xsi:type="xsd:int">1</return>
        </ns1:getNumberResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

但我需要这个:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://localhost/soap/dummy" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <SOAP-ENV:Body>
        <ns1:getNumberResponse>
            <return xsi:type="xsd:int">1</return>
        </ns1:getNumberResponse>
        <ns2:getNumberResponse>
            <return xsi:type="xsd:int">1</return>
        </ns2:getNumberResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

为什么我没有得到两个回应?

我搜索了SymfonyPHPZend的 SOAP 文档,以查看是否缺少某些配置选项,但找不到类似的东西。我在这里也没有成功搜索过这个问题。

4

1 回答 1

0

您是否尝试使用 zend 生成 WSDL?也许您的 WSDL 是错误的?

尝试使用 Zend 生成它,它应该可以工作,或者至少你可以看到你做错了什么:

use Symfony\Component\HttpFoundation\Response;
use Zend\Soap\AutoDiscover;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
....

/**
 * Generate a WSDL for the soap client.
 *
 * @Route("/soap/{client}", name="gateway_wsdl", defaults={"client"="default"}, requirements={
 *     "client"="default|some-client-id"
 * })
 * @param string $client
 * @return Response
 */
public function wsdlAction(string $client): ?Response
{
    $url = $this->generateUrl('gateway_server', ['client' => $client], UrlGeneratorInterface::ABSOLUTE_URL);

    // set up WSDL auto‑discovery
    $wsdl = new AutoDiscover();

    // attach SOAP service class
    $wsdl->setClass(SoapDummy::class);

    // set SOAP action URI
    $wsdl->setUri($url);

    // Generate a response
    $response = new Response();
    $response->headers->set('Content-Type', 'text/xml; charset=utf-8');
    $response->setContent($wsdl->toXml());

    return $response;
}
于 2019-12-05T12:04:02.470 回答