WCF SOAP request in PHP

6

I'm trying to consume a webservice from a shipping company. However, PHP is not mounting the request correctly.

PHP request:

<?php
$client = new SoapClient('http://data.../bidservice.svc?wsdl', [
    'soap_version' => SOAP_1_1,
    'trace'        => true,
]);

$arguments = [
    'Id'         => 1,
    'Total'      => 1,
    'Deliveries' => [
        'DeliveryBidRequest' => [
            'Id'          => 1,
            'Address'     => [
                'CityCode'       => '3551603',
                'Complement'     => '',
                'CustomerName'   => 'Daniel Bispo',
                'Document1'      => '30471410888',
                'Document2'      => '',
                'ICMSFree'       => '0',
                'Neighborhood'   => '3551603',
                'ReferencePoint' => '',
                'State'          => 'SP',
                'Street'         => 'Rua Herminio Alves de Godoi',
                'StreetNumber'   => '35',
                'ZipCode'        => '13930000',
            ],
            'Invoice'     => [
                'Amount' => 1200,
            ],
            'Operational' => [
                'CNPJ'          => '25027980000147',
                'CarrierCode'   => '0',
                'MinDays'       => '99',
                'OperationCode' => '1',
            ],
            'Volumes'     => [
                'VolumeBidRequest' => [
                    'Categories' => [
                        'CategoryBidRequest' => [
                            'Code' => 5,
                        ],
                    ],
                    'Properties' => [
                        'Alt'             => 0.21,
                        'Comp'            => 0.22,
                        'CubageM3'        => 0.369,
                        'Diam'            => 0.23,
                        'Larg'            => 0.24,
                        'MaxDim1Mt'       => 0,
                        'MaxDim2Mt'       => 0,
                        'QtdTotalVolumes' => 1,
                        'WeightKg'        => 12,
                    ],
                ],
            ],
        ],
    ],
];

try {
    $retorno = $client->__soapCall('PrimeiroBid', $arguments);
} catch (Exception $e) {
    $retorno = $e;
    // Salva a requisição enviada
    file_put_contents('erro.xml', $client->__getLastRequest());
}

echo '<pre>' . print_r($retorno, true) . '</pre>';

Response (error returned in exception)

The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation 'PrimeiroBid'. End element 'Body' from namespace 'http://schemas.xmlsoap.org/soap/envelope/' expected. Found element 'param1' from namespace ''. Line 2, position 152.

How the Request should go (template)

<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Header>
        <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IBidService/PrimeiroBid</Action>
    </s:Header>
    <s:Body>
        <PrimeiroBid xmlns="http://tempuri.org/">
            <request xmlns:d4p1="http://schemas.datacontract.org/2004/07/Pattio.Service.Soap" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                <d4p1:Deliveries>
                    <d4p1:DeliveryBidRequest>
                        <d4p1:Address>
                            <d4p1:CityCode>3550308</d4p1:CityCode>
                            <d4p1:Complement>CASA </d4p1:Complement>
                            <d4p1:CustomerName>DANIEL DA SILVA</d4p1:CustomerName>
                            <d4p1:Document1>28262505824</d4p1:Document1>
                            <d4p1:Document2 i:nil="true" />
                            <d4p1:ICMSFree>0</d4p1:ICMSFree>
                            <d4p1:Neighborhood>CENTRO</d4p1:Neighborhood>
                            <d4p1:ReferencePoint>ESQUINA</d4p1:ReferencePoint>
                            <d4p1:State>SP</d4p1:State>
                            <d4p1:Street>AMERICO DE MOURA</d4p1:Street>
                            <d4p1:StreetNumber>123</d4p1:StreetNumber>
                            <d4p1:ZipCode>13087601</d4p1:ZipCode>
                        </d4p1:Address>
                        <d4p1:Id>1</d4p1:Id>
                        <d4p1:Invoice>
                            <d4p1:Amount>1200</d4p1:Amount>
                        </d4p1:Invoice>
                        <d4p1:Operational>
                            <d4p1:CNPJ>25027980000147</d4p1:CNPJ>
                            <d4p1:CarrierCode>0</d4p1:CarrierCode>
                            <d4p1:MinDays>99</d4p1:MinDays>
                            <d4p1:OperationCode>1</d4p1:OperationCode>
                        </d4p1:Operational>
                        <d4p1:Volumes>
                            <d4p1:VolumeBidRequest>
                                <d4p1:Categories>
                                    <d4p1:CategoryBidRequest>
                                        <d4p1:Code>5</d4p1:Code>
                                    </d4p1:CategoryBidRequest>
                                </d4p1:Categories>
                                <d4p1:Properties>
                                    <d4p1:Alt>0.21</d4p1:Alt>
                                    <d4p1:Comp>0.22</d4p1:Comp>
                                    <d4p1:CubageM3>0.369</d4p1:CubageM3>
                                    <d4p1:Diam>0.23</d4p1:Diam>
                                    <d4p1:Larg>0.24</d4p1:Larg>
                                    <d4p1:MaxDim1Mt>0</d4p1:MaxDim1Mt>
                                    <d4p1:MaxDim2Mt>0</d4p1:MaxDim2Mt>
                                    <d4p1:QtdTotalVolumes>1</d4p1:QtdTotalVolumes>
                                    <d4p1:WeightKg>12</d4p1:WeightKg>
                                </d4p1:Properties>
                            </d4p1:VolumeBidRequest>
                        </d4p1:Volumes>
                    </d4p1:DeliveryBidRequest>
                </d4p1:Deliveries>
                <d4p1:Id>1</d4p1:Id>
                <d4p1:Total>1</d4p1:Total>
            </request>
        </PrimeiroBid>
    </s:Body>
</s:Envelope>

How the Request is Going (Confirmed by the carrier that is getting there like this)

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/">
    <SOAP-ENV:Body>
        <ns1:PrimeiroBid/>
        <param1>1</param1>
        <param2>
            <item>
                <key>DeliveryBidRequest</key>
                <value>
                    <item>
                        <key>Id</key>
                        <value>1</value>
                    </item>
                    <item>
                        <key>Address</key>
                        <value>
                            <item>
                                <key>CityCode</key>
                                <value>3551603</value>
                            </item>
                            <item>
                                <key>Complement</key>
                                <value></value>
                            </item>
                            <item>
                                <key>CustomerName</key>
                                <value>Daniel Bispo</value>
                            </item>
                            <item>
                                <key>Document1</key>
                                <value>30471410888</value>
                            </item>
                            <item>
                                <key>Document2</key>
                                <value></value>
                            </item>
                            <item>
                                <key>ICMSFree</key>
                                <value>0</value>
                            </item>
                            <item>
                                <key>Neighborhood</key>
                                <value>3551603</value>
                            </item>
                            <item>
                                <key>ReferencePoint</key>
                                <value></value>
                            </item>
                            <item>
                                <key>State</key>
                                <value>SP</value>
                            </item>
                            <item>
                                <key>Street</key>
                                <value>Rua Herminio Alves de Godoi</value>
                            </item>
                            <item>
                                <key>StreetNumber</key>
                                <value>35</value>
                            </item>
                            <item>
                                <key>ZipCode</key>
                                <value>13930000</value>
                            </item>
                        </value>
                    </item>
                    <item>
                        <key>Invoice</key>
                        <value>
                            <item>
                                <key>Amount</key>
                                <value>1200</value>
                            </item>
                        </value>
                    </item>
                    <item>
                        <key>Operational</key>
                        <value>
                            <item>
                                <key>CNPJ</key>
                                <value>25027980000147</value>
                            </item>
                            <item>
                                <key>CarrierCode</key>
                                <value>0</value>
                            </item>
                            <item>
                                <key>MinDays</key>
                                <value>99</value>
                            </item>
                            <item>
                                <key>OperationCode</key>
                                <value>1</value>
                            </item>
                        </value>
                    </item>
                    <item>
                        <key>Volumes</key>
                        <value>
                            <item>
                                <key>VolumeBidRequest</key>
                                <value>
                                    <item>
                                        <key>Categories</key>
                                        <value>
                                            <item>
                                                <key>CategoryBidRequest</key>
                                                <value>
                                                    <item>
                                                        <key>Code</key>
                                                        <value>5</value>
                                                    </item>
                                                </value>
                                            </item>
                                        </value>
                                    </item>
                                    <item>
                                        <key>Properties</key>
                                        <value>
                                            <item>
                                                <key>Alt</key>
                                                <value>0.21</value>
                                            </item>
                                            <item>
                                                <key>Comp</key>
                                                <value>0.22</value>
                                            </item>
                                            <item>
                                                <key>CubageM3</key>
                                                <value>0.369</value>
                                            </item>
                                            <item>
                                                <key>Diam</key>
                                                <value>0.23</value>
                                            </item>
                                            <item>
                                                <key>Larg</key>
                                                <value>0.24</value>
                                            </item>
                                            <item>
                                                <key>MaxDim1Mt</key>
                                                <value>0</value>
                                            </item>
                                            <item>
                                                <key>MaxDim2Mt</key>
                                                <value>0</value>
                                            </item>
                                            <item>
                                                <key>QtdTotalVolumes</key>
                                                <value>1</value>
                                            </item>
                                            <item>
                                                <key>WeightKg</key>
                                                <value>12</value>
                                            </item>
                                        </value>
                                    </item>
                                </value>
                            </item>
                        </value>
                    </item>
                </value>
            </item>
        </param2>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

How do I set PHP to send data correctly in the expected format?

Temporary link to roles and types: link

Obs :

  • SOAP Server Version 1.1
  • Client PHP Version: 7.1
asked by anonymous 31.08.2017 / 14:09

1 answer

2

As you are dealing with Complex Type , the simplest way is to mount the XML request part (without BODY or HEADER) and send directly using SoapVar.

Here is an example, based on the template you posted:

<?php
$xml = <<<'XML'
<PrimeiroBid xmlns="http://tempuri.org/">
    <request xmlns:d4p1="http://schemas.datacontract.org/2004/07/Pattio.Service.Soap" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <d4p1:Deliveries>
            <d4p1:DeliveryBidRequest>
                <d4p1:Address>
                    <d4p1:CityCode>3550308</d4p1:CityCode>
                    <d4p1:Complement>CASA </d4p1:Complement>
                    <d4p1:CustomerName>DANIEL DA SILVA</d4p1:CustomerName> 
                    <d4p1:Document1>28262505824</d4p1:Document1> 
                    <d4p1:Document2 i:nil="true" /> 
                    <d4p1:ICMSFree>0</d4p1:ICMSFree>
                    <d4p1:Neighborhood>CENTRO</d4p1:Neighborhood> 
                    <d4p1:ReferencePoint>ESQUINA</d4p1:ReferencePoint> 
                    <d4p1:State>SP</d4p1:State> 
                    <d4p1:Street>AMERICO DE MOURA</d4p1:Street> 
                    <d4p1:StreetNumber>123</d4p1:StreetNumber> 
                    <d4p1:ZipCode>13087601</d4p1:ZipCode> 
                </d4p1:Address>
                <d4p1:Id>1</d4p1:Id>
                <d4p1:Invoice>
                    <d4p1:Amount>1200</d4p1:Amount> 
                </d4p1:Invoice>
                <d4p1:Operational>
                    <d4p1:CNPJ>25027980000147</d4p1:CNPJ>
                    <d4p1:CarrierCode>0</d4p1:CarrierCode> 
                    <d4p1:MinDays>99</d4p1:MinDays> 
                    <d4p1:OperationCode>1</d4p1:OperationCode> 
                </d4p1:Operational>
                <d4p1:Volumes>
                    <d4p1:VolumeBidRequest>
                        <d4p1:Categories>
                            <d4p1:CategoryBidRequest>
                                <d4p1:Code>5</d4p1:Code> 
                            </d4p1:CategoryBidRequest>
                        </d4p1:Categories>
                        <d4p1:Properties>
                            <d4p1:Alt>0.21</d4p1:Alt>
                            <d4p1:Comp>0.22</d4p1:Comp>
                            <d4p1:CubageM3>0.369</d4p1:CubageM3> 
                            <d4p1:Diam>0.23</d4p1:Diam> 
                            <d4p1:Larg>0.24</d4p1:Larg> 
                            <d4p1:MaxDim1Mt>0</d4p1:MaxDim1Mt> 
                            <d4p1:MaxDim2Mt>0</d4p1:MaxDim2Mt> 
                            <d4p1:QtdTotalVolumes>1</d4p1:QtdTotalVolumes> 
                            <d4p1:WeightKg>12</d4p1:WeightKg> 
                        </d4p1:Properties>
                    </d4p1:VolumeBidRequest>
                </d4p1:Volumes>
            </d4p1:DeliveryBidRequest>
        </d4p1:Deliveries>
        <d4p1:Id>1</d4p1:Id> 
        <d4p1:Total>1</d4p1:Total> 
    </request>
</PrimeiroBid>
XML;

// Inicia o cliente
$client = new SoapClient('http://data.../bidservice.svc?wsdl', [
    'soap_version' => SOAP_1_1,
    'trace'        => true,
]);

// Transforma o XML em algo legível para o SOAP
$request = new SoapVar($xml, XSD_ANYXML);

try {
    // Pega o retorno da requisição em caso de sucesso
    $retorno = $client->PrimeiroBid($request);
} catch (Exception $e) {
    // Pega a exceção em caso de falha
    $retorno = $e;
}

// Salva a requisição enviada em um arquivo XML
file_put_contents('log.xml', $client->__getLastRequest());

// Mostra o conteúdo retornado, quer seja erro, quer acerto
echo '<pre>' . print_r($retorno, true) . '</pre>';

I believe that this should work.

Obs : Of course, if you are going to use variables internally in the XML assembly, remove the quotation marks in the HEREDOC call. Thus: $xml = <<<XML . Then you just use the variables normally, as you would in double quotation marks. Example: <d4p1:Amount>{$valor}</d4p1:Amount>

    
05.09.2017 / 15:30