0

How can I force my PHP SoapServer to send a JSON object as a response instead of an XML doc?

Thanks.

gipouf
  • 1,221
  • 3
  • 15
  • 43

2 Answers2

2

That is not SOAP, so no. It can incorporate a jsonstring in some xml node, that's about it. You may want just a REST server serving json.

You can bastardize it though, making it by definition NOT SOAP, but some weird hybrid:

<?php
class ThisIsNotASoapServer extends SoapServer {

}
function test(){
        //should have a return
        //return range(1,9);
        //but totally breaks it by:
        echo json_encode(range(1,9));
        //the exit here is needed
        exit;
}
$server = new ThisIsNotASoapServer(null, array('uri' => 'http://test-uri/','soap_version' => 1));
$server->addFunction("test");
$server->handle('<?xml version="1.0"?>
<SOAP-ENV:Envelope
   xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
   SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <SOAP-ENV:Body>
        <m:Test xmlns:m="Some-URI"/>
    </SOAP-ENV:Body>
 </SOAP-ENV:Envelope>');
?>

... so, technically this is possible, but I suspect there is not a single client which understands this.

Wrikken
  • 69,272
  • 8
  • 97
  • 136
  • Thanks, I'll look for it. But as far as i know, a web-service can be called a SOAP web-service even if the response is not in XML format. – gipouf Dec 15 '12 at 01:13
  • You'd better go and edit the Wikipedia page for SOAP then. SOAP is far too heavily reliant on XML to ever be used with JSON natively. Like the answer says, wrap it in XML or use RESTful state instead. – Ian Atkin Dec 15 '12 at 01:22
0

Take a look at http://www.sinatrarb.com its very easy to create a restful web service to return a json object.

depends on your requirements - SOAP is a xml request it doesnt mean the format for the return also needs to be SOAP (XML) you can easily post back a JSON string.

Maybe if you can provide more information we can help more?

Oliver Atkinson
  • 7,970
  • 32
  • 43
  • I'm using PHP SoapServer, and i could not find any documentation which shows how to change the return format of the service methods. I just if anyone knows what are the options for that... – gipouf Dec 15 '12 at 01:09
  • I know that it is possible in .Net – gipouf Dec 15 '12 at 01:10
  • Hm [this states](http://www.w3.org/TR/2000/NOTE-SOAP-20000508/#_Toc478383490) _"All SOAP messages are encoded using XML"_, has anything drastically changed in 1.2? – Wrikken Dec 15 '12 at 01:22
  • @Wrikken thats the request - the response can be in any format – Oliver Atkinson Dec 15 '12 at 13:20
  • @OliverAtkinson: I doubt it. WSDL both describes request and response data structures in XML. There simply is no place for JSON in here. At least my PHP Soap client heavily complains if the response is not XML: "Looks like we got no XML document". If you are right, can you point to some standard document that supports your view? – Sven Dec 15 '12 at 17:36