php - 将 xmlns 值添加到 Symfony2 SoapServer 响应

标签 php symfony wsdl soapserver

我们已经构建了一个 SOAP 服务来接收请求,但是给出的响应需要包含一个附加值,我们不知道如何添加它。

我们需要添加 xmlns="http://some-url.co.uk/"进入这个: <SOAP-ENV:PrintLocationResponse> 所以它读起来像这样 <SOAP-ENV:PrintLocationResponse xmlns="http://some-url.co.uk/">

SOAP 服务收到此请求:

<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <PrintLocation xmlns="http://some-url.co.uk/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <alertMsg>
                [ some message goes here]
            </alertMsg>
        </PrintLocation>
    </s:Body>
</s:Envelope>

SOAP 服务响应如下:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Body>
        <SOAP-ENV:PrintLocationResponse>
            <PrintLocationResult>
                Message contents goes here
            </PrintLocationResult>
        </SOAP-ENV:PrintLocationResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我们需要添加 xmlns="http://some-url.co.uk/"对这部分回复: <SOAP-ENV:PrintLocationResponse> 以便响应如下:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Body>
        <SOAP-ENV:PrintLocationResponse xmlns="http://some-url.co.uk/">
            <PrintLocationResult>
                Message contents goes here
            </PrintLocationResult>
        </SOAP-ENV:PrintLocationResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我们不知道如何添加 xmlns="http://some-url.co.uk/"进入上述回复。

管理请求并返回响应的 Controller 如下:
    public function indexAction(Request $request)
    {

        $soapServer = new \SoapServer('wsdl/hello.wsdl');
        $soapServer->setObject($this->get('hello_service'));

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

        ob_start();
        $soapServer->handle();

        return $response;

    }

$soapServer = new \SoapServer('wsdl/hello.wsdl');调用 你好.wsdl 看起来像这样:
<?xml version="1.0" encoding="ISO-8859-1"?>
<wsdl:definitions
             xmlns:SOAP-ENV = "http://www.w3.org/2001/12/soap-envelope"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema"
             xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
             xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
             >

    <wsdl:message name="helloIn">
        <part name="alertMsg" type="xsd:string" />
    </wsdl:message>

    <wsdl:message name="helloOut">
        <part name="PrintLocationResult" type="xsd:string" />
    </wsdl:message>

    <wsdl:portType name="hellowsdlPortType">
        <wsdl:operation name="PrintLocation">
            <wsdl:input message="tns:helloIn"/>
            <wsdl:output message="tns:helloOut"/>
        </wsdl:operation>
    </wsdl:portType>

    <wsdl:binding name="hellowsdlBinding" type="tns:hellowsdlPortType">
        <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="PrintLocation">

            <soap:operation soapAction="http://some-url.co.uk/PrintLocation" style="rpc"/>

            <wsdl:input>
                <soap:body use="literal" />
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal" />
            </wsdl:output>

        </wsdl:operation>
    </wsdl:binding>

    <wsdl:service name="hellowsdl">
        <wsdl:port name="hellowsdlPort" binding="tns:hellowsdlBinding">
            <soap:address location="http://backoffice.system/soap" />
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

上面的 WSDL 对我们来说有点神秘。我们修改了此处找到的示例:https://symfony.com/doc/2.7/controller/soap_web_service.html

$soapServer->setObject($this->get('hello_service'));调用 HelloService.php,它看起来像这样:
class HelloService
{
    /**
     * @var EntityManager
     */
    private $entityManager;

    public function __construct(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    public function PrintLocation($msg)
    {
        $log = new TempIressMsgThree($msg);
        $xml = simplexml_load_string($log->getMsg());
        $json = json_encode($xml);
        $array = json_decode($json,TRUE);

        $xml='<?xml version="1.0" encoding="UTF-8"?>
                <message xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.origoservices.com">
                    <m_control>
                       Response message contents goes here.
                    </m_control>
                </message>';

        return $xml;
    }

任何人都可以帮忙吗?

为了澄清,我们需要添加 xmlns="http://some-url.co.uk/"进入这个: <SOAP-ENV:PrintLocationResponse> 所以它读起来像这样 <SOAP-ENV:PrintLocationResponse xmlns="http://some-url.co.uk/">

最佳答案

header("Content-type: text/xml");

$string = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<SOAP-ENV:PrintLocationResponse>
<PrintLocationResult>
Message contents goes here
</PrintLocationResult>
</SOAP-ENV:PrintLocationResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
XML;

$content = simplexml_load_string($string,'SimpleXMLElement',LIBXML_NONET);

foreach($content->getDocNamespaces(TRUE) as $shortcut=>$namespace){
    $content->registerXPathNamespace($shortcut,$namespace );
}

$PLR = $content[0]->xpath('//SOAP-ENV:PrintLocationResponse');

foreach($PLR as $response){
    $response->addAttribute('xmlns', 'http://some-url.co.uk/');
}

echo $content[0]->asXML();

关于php - 将 xmlns 值添加到 Symfony2 SoapServer 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59734739/

相关文章:

php - 将一个表的一列连接到另一个表的多列

php - 无法打开输入文件应用程序/控制台

php - 访问 Twig 中字符串变量错误的属性

Symfony - SeoBundle 构建的 EMPTY 站点地图

php - 非 WSDL 模式下的 SOAP 问题

php - 如何批准待处理记录?

php - Asterisk 执行失败 '/usr/share/asterisk/agi-bin/call_handle.php' : Permission denied

php - 在ssh session 中访问Google引擎灵活的应用文件?

java - Java 中的 SOAP 错误处理

java - 使用 JAX-WS 绑定(bind)自定义重命名具有相同名称的 WSDL 元素