0

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

Thanks.

1
  • That’s not SOAP. Sounds like you want a more RESTful API. Commented Dec 15, 2012 at 1:25

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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

5 Comments

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...
I know that it is possible in .Net
Hm this states "All SOAP messages are encoded using XML", has anything drastically changed in 1.2?
@Wrikken thats the request - the response can be in any format
@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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.