0

I'm creating SOAP web service using PHP.

Here is my code..

SoapServer.php

    class server{

        public function RegisterComplaint($strInputXml){
            $str = "<RESULT><complaintNo>09865678</complaintNo></RESULT>";
            $arr['RegisterComplaintResult'] = trim($str);
            return $arr;
        }

    }

    $custom_wsdl = 'custom.wsdl';
    $server = new SoapServer($custom_wsdl);


    $server->setClass('server');
    $server->handle();

When I call RegisterComplaint using Wizdler(chrome extension) I get below result:

    <?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.Insurer.com/webservices/">
        <SOAP-ENV:Body>
            <ns1:RegisterComplaintResponse>
                <ns1:RegisterComplaintResult>&lt;RESULT&gt;&lt;complaintNo&gt;09865678&lt;/complaintNo&gt;&lt;/RESULT&gt;</ns1:RegisterComplaintResult>
            </ns1:RegisterComplaintResponse>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

Here I want result in below format(special characters to HTML entities):

    <?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.Insurer.com/webservices/">
        <SOAP-ENV:Body>
            <ns1:RegisterComplaintResponse>
                <ns1:RegisterComplaintResult><RESULT><complaintNo>09865678</complaintNo></RESULT></ns1:RegisterComplaintResult>
            </ns1:RegisterComplaintResponse>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

Does any one know what I have to change for required output?

I tried html_entity_decode() & htmlspecialchars() on $str variable, but it's not working.

Mak_091
  • 187
  • 1
  • 2
  • 14
  • That 's true, because the `SoapServer` class awaits an object as return value, which will be encoded to xml automatically under the use of the definitions in the wsdl file. A string return will always be encoded. Just use objects. – Marcel Dec 03 '18 at 13:29
  • Thanks Marcel for your help. It work's now.. – Mak_091 Dec 04 '18 at 05:39

1 Answers1

2

The solution as response. (Already quoted out in the comments)

The SoapServer class awaits an object as return value. This object will be automatically encoded by the server using the definitions from the used wsdl file. If a string is returned, its entities will always be encoded.

class Server
{
    public function registerComplaint()
    {
        $registerComplaintResponse = new stdClass();
        $registerComplaintResult = new stdClass();
        $result = new \stdClass();

        $result->complaintNo = '09865678';
        $registerComplaintResult->RESULT = $result;
        $registerComplaintResponse->RegisterComplaintResult = $registerComplaintResult;

        return $registerComplaintResponse;
    }
}

All definitions of return types (complex types) are defined in the wsdl file.

Marcel
  • 4,854
  • 1
  • 14
  • 24