【问题标题】:PHP - Use multiple xmlns in Envelope with SoapClientPHP - 在带有 SoapClient 的 Envelope 中使用多个 xmlns
【发布时间】:2015-10-19 07:58:34
【问题描述】:

我正在尝试在 SOAP 调用信封中使用多个 xmlns 命名空间进行 SOAP 调用,但我不知道如何正确执行此操作...

这是我现在拥有的代码:

$soapClient = new SoapClient(WSDL_URL, array(
            "trace" => true,
            "exceptions" => true
        ));
$soapClient->__setLocation(WSDL_LOCATION);

$request = '
        <ns1:someNodeName xmlns="http://some.webservice.url.com">
            <ns2:someOtherNodeName>
                // [REQUEST DETAILS]
            </ns2:someOtherNodeName>
        </ns1:someNodeName>
    ';

$params = new SoapVar($request, XSD_ANYXML);

try {
    $results = $soapClient->someFunctionName($params);
    return $results;
}
catch (Exception $e) {
    $error_xml =  $soapClient->__getLastRequest();
    echo $error_xml . "\n";
    echo $e->getMessage() . "\n";
}

这段代码给了我一个如下的 XML 请求:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://some.webservice.url.com">
    <SOAP-ENV:Body>
        <ns1:someNodeName xmlns="http://some.webservice.url.com">
            <ns2:someOtherNodeName>
                // [REQUEST DETAILS]
            </ns2:someOtherNodeName>
        </ns1:someNodeName>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我想要改变的是信封线,得到类似的东西:

<SOAP-ENV:Envelope 
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:ns1="http://some.webservice.url.com" 
    xmlns:ns2="http://some.other.webservice.url.com"
>

请问有什么方法可以实现吗?

【问题讨论】:

    标签: php xml soap namespaces soap-client


    【解决方案1】:

    XMLNS 属性是命名空间定义,如果您在该命名空间中添加节点,则会添加它们。它们不需要在根元素上,而是在使用它的元素或其祖先之一上。

    $request = '
        <ns1:someNodeName 
           xmlns:ns1="http://some.webservice.url.com"                 
           xmlns:ns2="http://some.other.webservice.url.com">
            <ns2:someOtherNodeName>
                // [REQUEST DETAILS]
            </ns2:someOtherNodeName>
        </ns1:someNodeName>
    ';
    

    如果您动态创建 XML,您应该使用 XML 扩展,如 DOM 或 XMLWriter。他们有特定的方法来创建带有命名空间的元素,并且会自动添加定义。

    $xmlns = [
      'ns1' => "http://some.webservice.url.com",                 
      'ns2' => "http://some.other.webservice.url.com"
    ];
    
    $document = new DOMDocument();
    $outer = $document->appendChild(
      $document->createElementNS($xmlns['ns1'], 'ns1:someNodeName')
    );
    $inner = $outer->appendChild(
      $document->createElementNS($xmlns['ns2'], 'ns2:someOtherNodeName')
    );
    $inner->appendChild(
      $document->createComment('[REQUEST DETAILS]')
    );
    
    $document->formatOutput = TRUE;
    echo $document->saveXml($outer);
    

    输出:

    <ns1:someNodeName xmlns:ns1="http://some.webservice.url.com">
      <ns2:someOtherNodeName xmlns:ns2="http://some.other.webservice.url.com">
        <!--[REQUEST DETAILS]-->
      </ns2:someOtherNodeName>
    </ns1:someNodeName>
    

    【讨论】:

    • 非常感谢。像魅力一样工作!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-06
    • 2013-01-04
    相关资源
    最近更新 更多