Consume webservice Soap with PHP (xml with attributes)

Asked

Viewed 1,837 times

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();
        }
  • using Curl is no less difficult? which return you will receive?

1 answer

0


Although it is a question that has been in the OS for a while, 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 returning) do not have attributes, as WS does not support them (with a single exception RPC/Encoded, but there is something very specific).

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

In this case, the parameters shall be sent in a similar way 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')
    )
);

Due to not having access to the full WSDL of your WS, another point that should be considered is to analyze whether the SOAP Binding is style (style) document or RPC (Remote Procedure Call). For, analyzing only the sending message (which is your example), the node receiveDocument can be both the name of the method (if the style be it RPC) how can be a method parameter (if the style be it document).

The form of consumption, shown in the previous code, is for when the binding for style="document". On the other hand, the call, with the parameters, to 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 $ex->getMessage();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.