Consume webservice soap with PHP (xml with attributes)

1

I need to consume a wsdl but the XML for sending to the webservice server has attributes. I created an array containing all tags, but I was wondering how to add the attributes of each tag.

XML to be sent to the server:

<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:ws='http://ws.document.general.modules.opengtm.com.br/'>
                    <soapenv:Header/>
                    <soapenv:Body>
                            <ws:receiveDocument>
                                    <ws:login>[email protected]</ws:login>
                                    <ws:password>123</ws:password>
                                    <ws:xmlDocument>
                                            <![CDATA[
                                                    <documents>
                                                            <document code='959559' operationDate='01/05/2018 12:21:00' isTicket='false' type='EN' fullWeight='38544' vehicleWeight='15870'>
                                                                    <owner document='23117229000106' name='SEND PHP' />
                                                                    <truck code='KAZ' serial='2931' />
                                                                    <carrier document='18822165000104' name='Reitran Transportes Ltda Me' />
                                                                    <item amount='1' lot='CAR' weight='22674'>
                                                                            <product code='102540' description='CAR BLACK' />
                                                                    </item>
                                                            </document>
                                                    </documents>
                                            ]]>
                                    </ws:xmlDocument>
                            </ws:receiveDocument>
                    </soapenv:Body>
                  </soapenv:Envelope>";

My Array

$lst = array('receiveDocument' => array("login" => "[email protected]",
                "password" => "123",
                "xmlDocument" => array(array("documents" => array("document" => (array('code' => '959580', 'operationDate' => '01/05/2018 12:21:00', 'isTicket' => 'false', 'type' => 'EN', 'fullWeight' => '38544', 'vehicleWeight' => '15870',
                        array(
                            array("owner" => array('document' => '23117229000106', 'name' => 'SEND PHP')),
                            array("truck" => array('code' => 'KAZ', 'serial' => '2931')),
                            array("carrier" => array('document' => '18822165000104', 'name' => 'Reitran Transportes Ltda Me')),
                            array("item" => array('amount' => '1', 'lot' => 'CAR', 'weight' => '22674', array("product" => array('code' => '102540', 'description' => 'CAR BLACK'))))))
                            ))))
            )
        );
        try {

            $url = 'http://192.168.0.89:8980/app/services/integrationDocument?WSDL';
            $webService = new \SoapClient($url);
            $result = $webService->receiveDocument($lst);
            dd($result);
        } catch (Exception $ex) {
            echo $exception->getMessage();
        }
    
asked by anonymous 09.05.2018 / 21:59

1 answer

0

Although this is an issue that has been in the OS for some time, it lacks an answer. Come on.

When consuming a WebService, you should be aware that the parameters defined in the WSDL (both for sending and for return) have no attributes, as WS does not support them (with a single exception RPC/Encoded ", but that's something very specific).

That is, the internal XML, which has attributes, is nothing more than a string of XML.

In this case, the parameters should be sent similarly to:

$xmlDocument = "<documents>
                    <document code='959559' operationDate='01/05/2018 12:21:00' isTicket='false' type='EN' fullWeight='38544' vehicleWeight='15870'>
                        <owner document='23117229000106' name='SEND PHP' />
                        <truck code='KAZ' serial='2931' />
                        <carrier document='18822165000104' name='Reitran Transportes Ltda Me' />
                        <item amount='1' lot='CAR' weight='22674'>
                            <product code='102540' description='CAR BLACK' />
                        </item>
                    </document>
                </documents>";

$lst = array(
    'receiveDocument' => array(
        "login" => "[email protected]",
        "password" => "123",
        "xmlDocument" => new \SoapParam(new \SoapVar($xmlDocument , XSD_ANYXML) , 'param')
    )
);

Because you do not have full WSDL access to your WS, another point to consider is whether the SOAP Binding is style ( style ) document or RPC ( Remote Procedure Call ). For example, by analyzing only the send message (which is its example), the receiveDocument node can be either the method name (if style is RPC ) or can be a method parameter (if style is document ).

The way to consume, presented in the previous code, is for when binding is style="document" . On the other hand, the call, with the parameters, for style="RPC" would be:

$xmlDocument = "<documents>
                    <document code='959559' operationDate='01/05/2018 12:21:00' isTicket='false' type='EN' fullWeight='38544' vehicleWeight='15870'>
                        <owner document='23117229000106' name='SEND PHP' />
                        <truck code='KAZ' serial='2931' />
                        <carrier document='18822165000104' name='Reitran Transportes Ltda Me' />
                        <item amount='1' lot='CAR' weight='22674'>
                            <product code='102540' description='CAR BLACK' />
                        </item>
                    </document>
                </documents>";

$xmlParam = new \SoapParam(new \SoapVar($xmlDocument , XSD_ANYXML) , 'param');

$login = "[email protected]";
$password = "123";


try {

    $url = 'http://192.168.0.89:8980/app/services/integrationDocument?WSDL';
    $webService = new \SoapClient($url);
    $result = $webService->receiveDocument($login , $password , $xmlParam);
    dd($result);
} catch (Exception $ex) {
    echo $exception->getMessage();
}
    
26.06.2018 / 15:25