How to reduce bytes number in SOAP response?
is there a way to reduce the number of bytes sent to the network from a SOAP response? I use nuSoap in PHP and would like to avo开发者_JAVA百科id xsi:type="xsd:string", xsi:type="xsd:int" in every node. Is there a way to do it?
Thanks in advance c.
If you are strictly tied to sending valid SOAP response back to Flash client, and if using valid schema is not an option (which would simplify output markup), then no, you can't do anything about it...
However, if you're NOT tied to sending valid SOAP response, and your developer really insists on getting non-verbose markup, then you are left with an alternative of rolling your own. If this is the case, read on.
You can use some of existing XML Serializer classes available via PEAR, or some other source. If you wanted to go down the Trully-Roll-Your-Own route, there are PHP classes to aid you in creating valid XML directly.
Not to repeat what's being said elsewhere, here's in-depth example about how to serialize some data using PEAR XML_Serializer.
Another really simple example, this time it's SimpleXML-based, where you add child elements and attributes as needed to meet desired output.
$xml = new SimpleXMLElement('<?xml version="1.0" standalone="yes"?>');
$ch1 = $xml->addChild("root");
$ch2 = $ch1->addChild("element");
// you obviously have no use for attributes, but I included it for completeness
$ch2->addAttribute("foo", "bar");
$ch2->addChild("subElement", "value1");
$outxml = $xml->asXML();
Code above would produce something like:
<?xml version="1.0" standalone="yes"?>
<root>
<element foo="bar">
<subElement>value1</subElement>
</element>
</root>
I hope it helps.
The usual way to do this is to enable compression in the HTTP layer. If you want to make sure that you're always compressing data, you could even reject SOAP requests that are not compressed. (A popular software Q&A site does this.)
Remove whitespaces and gzip the contents during transport.
header('Content-Encoding: gzip');
$xml=new DOMDocument($soap);
$xml->preserveWhiteSpace(FALSE);
echo $xml->saveXML();
精彩评论