Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
509 views
in Technique[技术] by (71.8m points)

change soap prefixes in php

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...