Search code examples
phpxmlsoapwsdl

Generating XML SOAP message in PHP


I need help generating XML SOAP message in PHP. I'm using BeSimpleSoap extension. I've tried many different ways to generate SOAP message. Message should look like this:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://apis-it.hr/umu/2015/types/kp" xmlns:gsb="http://apis-it.hr/umu/2013/types/gsb">
   <soapenv:Header/>
   <soapenv:Body>
      <gsb:SendMessageRequest>
         <gsb:GsbEnvelope>
            <gsb:MessageHeader>
               <gsb:SenderId>1</gsb:SenderId>
               <gsb:ServiceId>126</gsb:ServiceId>
               <gsb:MessageId>c4413331-1cff-11e2-f516-242d656ac4b3</gsb:MessageId>
               <gsb:SenderTimeStamp>2015-05-31T12:00:00</gsb:SenderTimeStamp>
            </gsb:MessageHeader>
            <gsb:Content>
               <gsb:MimeType>aaa</gsb:MimeType>
               <gsb:Data encoding="EMBEDDED">
                     <tns:Upit>
                        <tns:IdPosiljatelja>1</tns:IdPosiljatelja>
                        <tns:TipPoruke>1</tns:TipPoruke>
                        <tns:IdUpita>732262f1-063f-11e2-892e-0812200c9f68</tns:IdUpita>
                        <tns:DatumVrijemeUpita>2015-03-26T15:33:29.371+01:00</tns:DatumVrijemeUpita>
                     </tns:Upit>
               </gsb:Data>
            </gsb:Content>
         </gsb:GsbEnvelope>
      </gsb:SendMessageRequest>
   </soapenv:Body>
</soapenv:Envelope>

I have tried to generat XML with this:

$encodded = new SoapVar("<tns:Upit>
                                <tns:IdPosiljatelja>196</tns:IdPosiljatelja>
                                <tns:TipPoruke>$TipPoruke</tns:TipPoruke>
                                <tns:IdUpita>$UUID</tns:IdUpita>
                                <tns:DatumVrijemeUpita>$date_time</tns:DatumVrijemeUpita>
                            </tns:Upit>", XSD_ANYXML); 

    $par_envelope=array( "GsbEnvelope" =>  
        array( "MessageHeader" => 
            array("SenderId" => "24",
                "ServiceId" => "$ServiceId",
                "MessageId" => $UUID,
                "SenderTimeStamp" => $date_time),
                "Content" => array("MimeType" =>"application/xml","Data" =>array("any"=>$encodded,"encoding"=>"EMBEDDED"))));

    $par_WSDL=array("trace"=>TRUE,
        "exceptions"=>TRUE,
        'location'=>$SERVICE_TEST,
        "local_cert" =>$SOAP_cert,
        'uri'=>$NAMESPACE_URI,
        "passphrase"=>$cert_password,
        "connection_timeout" => 60);

    $client = new BeSimple\SoapClientSoapClient("GSBService.wsdl",$par_WSDL);

    $result=$client->sendMessage($par_envelope);

But that only generated part of a XML message that looks like:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://apis-it.hr/umu/2013/types/gsb">
    <SOAP-ENV:Body>
        <ns1:SendMessageRequest>
            <ns1:GsbEnvelope>
                <ns1:MessageHeader>
                    <ns1:SenderId>1</ns1:SenderId>
                    <ns1:ServiceId>1</ns1:ServiceId>
                    <ns1:MessageId>34578b73-988a-4727-bee4-a287218cc0a1</ns1:MessageId>
                    <ns1:SenderTimeStamp>2015-10-01T09:07:25+02:00</ns1:SenderTimeStamp>
                </ns1:MessageHeader>
                <ns1:Content>
                    <ns1:MimeType>application/xml</ns1:MimeType>
                    <ns1:Data/>
                </ns1:Content>
            </ns1:GsbEnvelope>
        </ns1:SendMessageRequest>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope> 

EDIT Here you can see WSDL and XSD files.


Solution

  • It seems that the library adds its own namespace prefixes. This is not a problem. In XML the following 3 examples have the same meaning:

    • <ns1:SendMessageRequest xmlns:ns1="http://apis-it.hr/umu/2013/types/gsb">
    • <gsb:SendMessageRequest xmlns:gsb="http://apis-it.hr/umu/2013/types/gsb">
    • <SendMessageRequest xmlns="http://apis-it.hr/umu/2013/types/gsb">

    The XML parser will resolve all 3 to an element node with the local name SendMessageRequest in the namespace http://apis-it.hr/umu/2013/types/gsb.

    So the changed prefix is not a problem. The library can do this to generate/optimize the output.

    But, your XML Soap variable is not valid XML. You did not add the namespace definition. If an element has a prefix you will need a namespace definition for this prefix on the element node or one of its ancestors.

    $encoded = new SoapVar(
      "<tns:Upit xmlns:tns='http://apis-it.hr/umu/2015/types/kp'>
         <tns:IdPosiljatelja>196</tns:IdPosiljatelja>
         <tns:TipPoruke>$TipPoruke</tns:TipPoruke>
         <tns:IdUpita>$UUID</tns:IdUpita> 
         <tns:DatumVrijemeUpita>$date_time</tns:DatumVrijemeUpita>
       </tns:Upit>", 
      XSD_ANYXML
    );