SOAP WCF request in PHP

Asked

Viewed 720 times

6

I am trying to consume a web service from a shipping company. However, PHP is not mounting the request correctly.

Request in PHP:

<?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 Request should go (model)

<?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 Request is doing (Confirmed by the carrier that is getting there this way)

<?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 to set up PHP to send the data correctly in the expected format?

Temporary link with functions and types: https://www.tmwxd.com.br/config/pattio/pegando_tipos.php

Obs:

  • SOAP Server Version 1.1
  • Client PHP Version: 7.1
  • Do you know how are the bindings configured in wcf? Or can consume the service with another tool?

  • @Grupocdsinformática what we got - besides XML with the model above - was this: https://www.tmwxd.com.br/config/pattio/pegando_tipos.php | When you say "other tools" what do you mean? I only know the class Soapclient()

  • What is the Soap Server version?

  • @Leonancarvalhonão I have no idea. To help the company does not have a documentation. The only thing they gave us was the model XML above. However, note that PHP’s Soapclient is sending a very atypical XML. The PHP version used is 7.1

  • 1

    @Szag-Ot tools like Soap UI, to try to communicate with the service. What you can do is, in Soapui, you add the same WSDL address of the service, it will show all the calls, and the XML you have to send. What I believe to be the solution, will be to pass the objects in php. If in the end nothing goes right, build XML manually and go to request.

  • @Grupocdsinformática I appreciate the information, but the problem is not in the test of the service itself. Yes, we use a similar tool that allows us to send the XML in the informed model. SOAP works perfectly. The problem, as stated above, is to send the objects through PHP. In the example I put as array, but the result is the same with object: The XML that Soapclient() sends is not the same as the model. I need to get Soapclient to mount XML in the correct format.

  • @Szag-Ot, try to see this link, if it helps. It’s from the OS in English.

  • @Grupocdsinformática Gratefully, but had already seen it. It does not apply to the current case.

  • I put the calls in full, for better understanding

  • @Guilhermenascimento Yes. The return trace (__getLastRequest()) declared under "How it’s coming" is exactly how it’s coming to the server, according to the carrier’s own message

  • @Guilhermenascimento is described in question: 7:1

  • @Guilhermenascimento No, that’s exactly what PHP is sending to SOAP Server. The return that Server is giving is another, in the case of a format error. SOAP-ENV Content is the result of __getLastRequest(). The result of the request itself is a format error. As already mentioned, the carrier staff has already confirmed the format that is getting there, and what they sent us is exactly the return of __getLastRequest()

  • Take a look at this question https://stackoverflow.com/a/15844623/3706998 E also on my client implementation, https://gist.github.com/LeonanCarvalho/2871c96f522ca56dd3aa37f0f139f97a I’m not sure if the problem is with your client itself, I believe it’s on the server.

  • Thanks @Leonancarvalho, but it does not apply to the case. There is no error on the server, since tests made with Soapui worked perfectly. It is simply something in the call of the Primeirobid function. One of the parameters says it should be in Arrayofstring format, as shown by the temporary link in the question...

Show 9 more comments

1 answer

2


How are you dealing with Complex Type, the simplest way is to assemble the request part of XML (without BODY or HEADER) and send it directly using Soapvar.

Here’s an example, based on the model 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 when mounting XML you are going to use variables internally, remove the quotes in the HEREDOC call. So: $xml = <<<XML. Then just use the variables normally, as you would inside double quotes. Example: <d4p1:Amount>{$valor}</d4p1:Amount>

  • 1

    Perfect! Thank you very much!

Browser other questions tagged

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