【问题标题】:php - SoapServer - Need add a namespace in Soap responsephp - SoapServer - 需要在 Soap 响应中添加命名空间
【发布时间】:2017-07-11 08:55:16
【问题描述】:

我需要在 Soap 响应中添加一个命名空间。我正在使用 php 和 SoapServer。我的回复是这样开始的:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns1="urn:query:request:v2.0">

我需要这样开始:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns1="urn:query:request:v2.0" xmlns:ns2="urn:query:type:v2.0">

我在PHP中的代码是这样的,我不知道如何继续:

class Service
{
// FUNCTIONS
}

$options= array('uri'=>'urn:query:request:v2.0',
    'cache_wsdl' => WSDL_CACHE_NONE);
$server=new SoapServer("Service.wsdl",$options);

$server->setClass('Service');
$server->addFunction(SOAP_FUNCTIONS_ALL);

$server->handle();

谢谢

【问题讨论】:

    标签: php namespaces soapserver


    【解决方案1】:

    命名空间被动态添加到soap响应体中。只要肥皂体中没有具有所需命名空间的元素,它就不会出现。您必须在响应中声明它。这是一个简单的例子。

    Soap 请求处理类

    通常在这个类中定义了soap服务的功能。这里发生了魔术。您可以使用所需的命名空间来初始化 SoapVar 对象。

    class Response
    {
        function getSomething()
        {
            $oResponse = new StdClass();
            $oResponse->bla = 'blubb';
            $oResponse->yadda = 'fubar';
    
            $oEncoded = new SoapVar(
                $oResponse,
                SOAP_ENC_OBJECT,
                null,
                null,
                'response',
                'urn:query:type:v2.0'
            );
    
            return $oEncoded;
        }
    }
    

    使用 PHP 自己的 SoapVar 类,您可以将命名空间添加到节点。第五个参数是节点的名称,第六个参数是节点所属的命名空间。

    Soap 服务器

    $oServer = new SoapServer(
        '/path/to/your.wsdl',
        [
            'encoding' => 'UTF-8',
            'send_errors' => true,
            'soap_version' => SOAP_1_2,
        ]
    );
    
    $oResponse = new Response();
    
    $oServer->setObject($oResponse);
    $oServer->handle();
    

    如果调用服务函数getSomething,响应将类似于以下xml。

    <?xml version="1.0" encoding="UTF-8"?>
    <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="urn:query:type:v2.0">
        <env:Body>
            <ns1:Response>
                <ns1:bla>blubb</ns1:yadda>
                <ns1:blubb>fubar</ns1:blubb>
            </ns1:Response>
        </env:Body>
    </env:Envelope>
    

    如您所见,我们提供给 SoapVar 对象的命名空间出现在肥皂响应的信封节点中。

    【讨论】:

    • 谢谢。您的回复非常有用。
    • 嗨!我想将xmlns:ns2="http://uws.provider.com/" 添加到&lt;ns1:Response&gt; 怎么做?
    • 使用上面显示的解决方案,所有使用的 xml 命名空间都会自动添加到信封中。不必为子节点添加命名空间声明。
    猜你喜欢
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    • 2017-06-10
    • 1970-01-01
    相关资源
    最近更新 更多