更改php中的soap前缀

发布于 2024-12-23 05:32:17 字数 1016 浏览 0 评论 0原文

我正在将一个soap web 服务从.net 重写为php。默认情况下,php 给我的标签看起来像这样:

<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>

等等...

但我需要这个:

<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 和 SOAP。改变信封但是我不想像他那样破解它。另外,我正在创建一个肥皂服务,该服务将由现有的 iPhone 应用程序使用,而不是使用 PHP 使用 SoapClient 来使用肥皂服务。 iPhone 应用程序只是解析原始 xml,我现在无法更改 iPhone 应用程序。

i'm rewriting a soap web service from .net to php. by default, php is giving me tags that look like this:

<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>

etc...

but i need this:

<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>

This is similar to the question here: PHP AND SOAP. Change envelope however i'd like to not hack it the way he did. Also, i am creating a soap service that will be consumed by an existing iphone app, not using PHP to consume a soap service using SoapClient. The iphone app just parses the raw xml and i can't change the iphone app right now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柒七 2024-12-30 05:32:17

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

假设

  • 如果我是正确的,您会知道 SOAP 前缀本身不是'问题(只要一致,您就可以使用任何前缀)。
  • 我们需要为这个特定的 Iphone 应用程序创建一个解决方法,该应用程序使用当前无法由您修改/升级的 (xml) 解析器

您想要什么?

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

解决方案

  1. 目前没有用于更改 SOAP 前缀的本机 SoapServer API 方法
  2. 您可以捕获SoapServer 响应和过程通过正则表达式或 xml 解析器进行响应,请参阅下面的示例
<?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

?>

这应该可以解决问题,我还没有创建正则表达式部分或实际的 xlm 节点替换,但我认为你可以自己做

After re-reading what it is you want and searching through the php documentation here is my solution and a couple of assumptions that I made

Assumptions

  • If I'm correct you are aware that the SOAP prefix itself isn't the issue (you are allowed to use any prefix as long as it is consistent).
  • We need to create a work-around for this specific Iphone app that makes use of a (xml) parser that currently can not be modified/upgraded by you

What do you want?

  1. You would like to use a native API to alter the SOAP prefix
  2. You want to catch the SoapServer response so you can alter the SOAP prefix before it is being returned

Solution

  1. Currently there is no native SoapServer API method to alter the SOAP prefix
  2. You can catch the SoapServer response and process the response either by regex or xml parser see example below
<?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

?>

This should do the trick, I haven't created the regex part or the actual xlm node replacement but I figure you can do that yourself

梦毁影碎の 2024-12-30 05:32:17

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

<?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);
        }
    }
}

If you don't want to use Regular expressions, I've done this quick class implementation which uses DomXPath and DomDocument to cleanup the XML and append the namespace attribute at the node level.

<?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);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文