首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >更改php中的soap前缀

更改php中的soap前缀
EN

Stack Overflow用户
提问于 2011-12-28 08:09:10
回答 2查看 14.3K关注 0票数 10

我正在把soap web服务从.net重写成php。默认情况下,php给我的标签是这样的:

代码语言:javascript
复制
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/"><SOAP-ENV:Header><ns1:FindAllCategories/></SOAP-ENV:Header><SOAP-ENV:Body><ns1:FindAllCategoriesResponse><ns1:FindAllCategoriesResult><ns1:ArtistCategoryDto>

等等。

但我需要这个:

代码语言:javascript
复制
<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><FindAllCategoriesResponse xmlns="http://tempuri.org/"><FindAllCategoriesResult><ArtistCategoryDto>

这类似于这里的问题:PHP AND SOAP. Change envelope,但是我不想像他那样破解它。此外,我正在创建一个soap服务,它将由现有的iphone应用程序使用,而不是通过SoapClient使用PHP来使用soap服务。iphone应用程序只能解析原始的xml,我现在不能更改iphone应用程序。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-01-06 04:22:17

在重新阅读您想要的内容并搜索php文档之后,这里是我的解决方案和我所做的几个假设。

假设

如果我是正确的,您应该知道SOAP前缀本身并不是问题所在(您可以使用任何前缀,只要它是consistent).

  • We的,您就需要为这个特定的应用程序创建一个变通方法,该应用程序使用了当前不能由您修改/升级的(
  • )解析器

你想要什么?

  1. 您希望使用本机API来更改SOAP前缀
  2. 您希望捕获SoapServer响应,以便可以在返回SOAP前缀之前更改它

解决方案

  1. 当前没有本机SoapServer API方法来更改SOAP前缀
  2. 您可以捕获SoapServer响应并通过正则表达式或xml解析器处理响应请参阅下面的示例

代码语言:javascript
复制
<?php

// Create you parse function - Regex
function SoapServerRegexParser($input)
{
    // $input contains your XML Response
    // Do str_replace or preg_replace   
    $request = preg_replace({do replace});

    //return modified output to client
    return $request;
}

// OR create you parse function - Regex XML Parser
function SoapServerXMLParser($input)
{
    // $input contains your XML Response
    // Use any xml parser that you would like   
    $xml = new DOMDocument();
    $xml->formatOutput = true;
    $xml->preserveWhiteSpace = false;
    $xml->loadXML($input);
    //Do replacement have a looke at: DOMNode::replaceChild

    //return modified output to client
    return $xml->saveXML();
}

// Make php buffer all output
// Send all output to a callBack function

// Replace 'SoapServerRegexParser' with the callback function name of choice
ob_start('SoapServerRegexParser'); //buffer output and set callback function

// Create SoapServer
$server = new SoapServer('wsdlfile.wsdl');
$server->handle(); //Handle incoming request
ob_end_flush(); //Release buffer, but send through callback function first

?>

这应该可以做到这一点,我还没有创建regex部分或实际的xlm节点替换,但我认为您可以自己完成

票数 5
EN

Stack Overflow用户

发布于 2012-01-04 20:39:37

如果您不想使用正则表达式,我已经完成了这个快速的类实现,它使用DomXPath和DomDocument清理XML并在节点级别附加名称空间属性。

代码语言:javascript
复制
<?php

public BetterSoapClient extends SoapClient {

    public function __construct($wsdl, $options = null) {
        parent::__construct($wsdl, $options);
    }

    public function __doRequest($request, $location, $action, $version) {

        $dom = new DOMDocument('1.0');

        // loads the SOAP request to the Document
        $dom->loadXML($request);

        // Create a XPath object
        $path = new DOMXPath($dom);

        // Search the nodes to fix
        $nodesToFix = $path->query('//SOAP-ENV:Envelope/SOAP-ENV:Body/*', null, true);

        // Remove unwanted namespaces
        $this->fixNamespace($nodesToFix, 'ns1', 'http://tempuri.org/');

        // Save the modified SOAP request
        $request = $dom->saveXML();

        return parent::__doRequest($request, $location, $action, $version);
    }

    public function fixNamespace(DOMNodeList $nodes, $namespace, $value) {
        // Remove namespace from envelope
        $nodes->item(0)
                ->ownerDocument
                ->firstChild
                ->removeAttributeNS($value, $namespace);

        //iterate through the node list and remove namespace

        foreach ($nodes as $node) {

            $nodeName = str_replace($namespace . ':', '', $node->nodeName);
            $newNode = $node->ownerDocument->createElement($nodeName);

            // Append namespace at the node level
            $newNode->setAttribute('xmlns', $value);

            // And replace former node
            $node->parentNode->replaceChild($newNode, $node);
        }
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8650580

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档