PHP - Consume webservice with XML via SOAP

Asked

Viewed 1,029 times

2

I’m having problems with array in php. I need to send an xml via Soap to the webservice with some product items, but a tag is bringing me questions. The wsdl manual has an xml like this:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.edei.correios.com.br/"> <soapenv:Header/>
<soapenv:Body>
    <ser:solicitarCodigoEtiquetaInternacional>
        <!--Optional:-->
        <xml>
            <remessa>
                <idioma>PT</idioma>
                <encomendas>
                    <encomenda>
                        <cartaoDePostagem>0061962740</cartaoDePostagem>
                        <codigoControleCliente>100052202</codigoControleCliente>
                        <nomeRemetente>magento.COM</nomeRemetente>
                        <enderecoRemetente>Street Test</enderecoRemetente>
                        <numeroEnderecoRemetente>2017</numeroEnderecoRemetente>
                        <complementoEnderecoRemetente></complementoEnderecoRemetente>
                        <codigoPostalRemetente>83642</codigoPostalRemetente>
                        <cidadeRemetente>MERIDIAN</cidadeRemetente>
                        <estadoRemetente>ID</estadoRemetente>
                        <paisRemetente>US</paisRemetente>
                        <emailRemetente>[email protected]</emailRemetente>
                        <telefoneRemetente></telefoneRemetente>
                        <siteRemetente>www.bodybuilding.com</siteRemetente>
                        <nomeDestinatario>Junior Maia</nomeDestinatario>
                        <tipoDocumentoDestinatario>1</tipoDocumentoDestinatario>
                        <numeroDocumentoDestinatario>16549651468</numeroDocumentoDestinatario>
                        <enderecoDestinatario>Street Test</enderecoDestinatario>
                        <numeroEnderecoDestinatario>S/N</numeroEnderecoDestinatario>
                        <complementoEnderecoDestinatario>Test</complementoEnderecoDestinatario>
                        <codigoPostalDestinatario>70680506</codigoPostalDestinatario>
                        <caixaPostalDestinatario></caixaPostalDestinatario>
                        <cidadeDestinatario>Curitiba</cidadeDestinatario>
                        <ufDestinatario>PR</ufDestinatario>
                        <paisDestinatario>BR</paisDestinatario>
                        <emailDestinatario>[email protected]</emailDestinatario>
                        <telefoneDestinatario></telefoneDestinatario>
                        <pesoObjeto>150</pesoObjeto>
                        <comprimentoObjeto>16</comprimentoObjeto>
                        <larguraObjeto>11</larguraObjeto>
                        <alturaObjeto>2</alturaObjeto>
                        <modalidadeDistribuicaoObjeto>33375</modalidadeDistribuicaoObjeto>
                        <modalidadePagamentoImpostosObjeto>1</modalidadePagamentoImpostosObjeto>
                        <codigoMoeda>USD</codigoMoeda>
                        <valorFreteObjeto>50.00</valorFreteObjeto>
                        <valorSeguroObjeto>0.00</valorSeguroObjeto>
                        <modoDevolucaoObjeto>1</modoDevolucaoObjeto>
                        <seguroDevolucaoObjeto>N</seguroDevolucaoObjeto>
                        <itensEncomenda>
                            <itemEncomenda>
                                <codigoSHItem>100190</codigoSHItem>
                                <codigoSKUItem>TYH820040</codigoSKUItem>
                                <descricaoItem>BPI SPORTS BLOX - 110 SERVINGS - BLUEBERRY</descricaoItem>
                                <quantidadeItem>1</quantidadeItem>
                                <valorUnitario>100.00</valorUnitario>
                                <pesoUnitario></pesoUnitario>
                            </itemEncomenda>
                            <itemEncomenda>
                                <codigoSHItem>100191</codigoSHItem>
                                <codigoSKUItem>TYH820041</codigoSKUItem>
                                <descricaoItem>BPI SPORTS BLOX - 110 SERVINGS - WHITE</descricaoItem>
                                <quantidadeItem>1</quantidadeItem>
                                <valorUnitario>100.00</valorUnitario>
                                <pesoUnitario></pesoUnitario>
                            </itemEncomenda>
                            <itemEncomenda>
                                <codigoSHItem>100192</codigoSHItem>
                                <codigoSKUItem>TYH820042</codigoSKUItem>
                                <descricaoItem>BPI SPORTS BLOX - 110 SERVINGS - RED</descricaoItem>
                                <quantidadeItem>1</quantidadeItem>
                                <valorUnitario>100.00</valorUnitario>
                                <pesoUnitario></pesoUnitario>
                            </itemEncomenda>
                        </itensEncomenda>
                    </encomenda>
                </encomendas>
            </remessa>]]>
        </xml>
    </ser:solicitarCodigoEtiquetaInternacional>
</soapenv:Body>

Note that the tag <itemEncomenda> repeats depending on how many products the request has, and that’s exactly what I can’t feed in php.

I’m using the StdClass to mount an array with the requested positions in the example and mount the xml with the SoapVar, is working for all tags, but for itemEncomenda no! For example, I have 4 items in order, only the last one is written on the tag <itemEncomenda>, others do not appear. I imagine this happens because an index itemEncomenda is created in the matrix and it overwrites the old positions. I really don’t know how to assemble an array repeating that tag.

Here’s my code to mount the xml with StdClasse mount the xml:

            $var = new StdClass;
            $var->xml = new stdClass;
            $var->xml->remessa = new stdClass;
            $var->xml->remessa->idioma = $this->idioma;
            $var->xml->remessa->encomendas = new stdClass;
            $var->xml->remessa->encomendas->encomenda = new stdClass;
            //remetende
            $var->xml->remessa->encomendas->encomenda->cartaoDePostagem  = $this->cartao_postagem;
            $var->xml->remessa->encomendas->encomenda->codigoControleCliente  = '';
            $var->xml->remessa->encomendas->encomenda->nomeRemetente = $this->remetente_nome;
            $var->xml->remessa->encomendas->encomenda->enderecoRemetente = $this->remetente_rua;
            $var->xml->remessa->encomendas->encomenda->numeroEnderecoRemetente = $this->remetente_numero;
            $var->xml->remessa->encomendas->encomenda->complementoEnderecoRemetente = $this->remetente_complemento;
            $var->xml->remessa->encomendas->encomenda->codigoPostalRemetente  = $this->remetente_cep;
            $var->xml->remessa->encomendas->encomenda->cidadeRemetente = $this->remetente_cidade;
            $var->xml->remessa->encomendas->encomenda->estadoRemetente = $this->remetente_estado;
            $var->xml->remessa->encomendas->encomenda->paisRemetente = $this->remetente_pais;
            $var->xml->remessa->encomendas->encomenda->emailRemetente = $this->remetente_email;
            $var->xml->remessa->encomendas->encomenda->telefoneRemetente = $this->remetente_telefone;
            $var->xml->remessa->encomendas->encomenda->siteRemetente = $this->remetente_site;
            //destinatário customer
            $var->xml->remessa->encomendas->encomenda->nomeDestinatario = $customer_nome;
            $var->xml->remessa->encomendas->encomenda->tipoDocumentoDestinatario = '1'; //1-cpf 2-cnpj 3-passport 4-not informed
            $var->xml->remessa->encomendas->encomenda->numeroDocumentoDestinatario = $customer_taxvat;
            $var->xml->remessa->encomendas->encomenda->enderecoDestinatario = $customer_rua;
            $var->xml->remessa->encomendas->encomenda->numeroEnderecoDestinatario = $customer_rua_numero;
            $var->xml->remessa->encomendas->encomenda->complementoEnderecoDestinatario = $customer_compemento;
            $var->xml->remessa->encomendas->encomenda->codigoPostalDestinatario = $customer_cep;
            $var->xml->remessa->encomendas->encomenda->caixaPostalDestinatario = '';
            $var->xml->remessa->encomendas->encomenda->cidadeDestinatario = $customer_cidade;
            $var->xml->remessa->encomendas->encomenda->ufDestinatario = $customer_uf;
            $var->xml->remessa->encomendas->encomenda->paisDestinatario = $customer_pais;
            $var->xml->remessa->encomendas->encomenda->emailDestinatario = $customer_email;
            $var->xml->remessa->encomendas->encomenda->telefoneDestinatario = $customer_telefone;
            //objeto
            $var->xml->remessa->encomendas->encomenda->pesoObjeto = '';
            $var->xml->remessa->encomendas->encomenda->comprimentoObjeto = '';
            $var->xml->remessa->encomendas->encomenda->larguraObjeto = '';
            $var->xml->remessa->encomendas->encomenda->alturaObjeto = '';
            $var->xml->remessa->encomendas->encomenda->modalidadeDistribuicaoObjeto = $this->modalidade_distribuicao_objeto;
            $var->xml->remessa->encomendas->encomenda->modalidadePagamentoImpostosObjeto = $this->modalidade_pagamento_imposto;
            $var->xml->remessa->encomendas->encomenda->codigoMoeda = $this->moeda;
            $var->xml->remessa->encomendas->encomenda->valorFreteObjeto = '';
            $var->xml->remessa->encomendas->encomenda->valorSeguroObjeto = '';
            $var->xml->remessa->encomendas->encomenda->modoDevolucaoObjeto = $this->modo_devolocao;
            $var->xml->remessa->encomendas->encomenda->seguroDevolucaoObjeto = $this->seguro_devolucao ;
            //itens
            $var->xml->remessa->encomendas->encomenda->itensEncomenda = new stdClass;

            foreach ($invoice->getAllItems() as $item){
                if ($item->getOrderItem()->getParentItem()) {
                    continue;
                }

                $item_name = $item->getName();
                $item_sku = $item->getSku();
                $item_id = $item->getProductId();
                $item_qty = $item->getQty();
                $item_price = $item->getPrice();
                $_product = Mage::getModel('catalog/product')->load($item_id);
                $item_peso = $_product->getWeight();

                $var->xml->remessa->encomendas->encomenda->itensEncomenda->itemEncomenda = new stdClass;
                $var->xml->remessa->encomendas->encomenda->itensEncomenda->itemEncomenda->codigoSHItem = '';
                $var->xml->remessa->encomendas->encomenda->itensEncomenda->itemEncomenda->codigoSKUItem = $item_sku;
                $var->xml->remessa->encomendas->encomenda->itensEncomenda->itemEncomenda->descricaoItem = $item_name;
                $var->xml->remessa->encomendas->encomenda->itensEncomenda->itemEncomenda->quantidadeItem = $item_qty;
                $var->xml->remessa->encomendas->encomenda->itensEncomenda->itemEncomenda->valorUnitario = $item_price;
                $var->xml->remessa->encomendas->encomenda->itensEncomenda->itemEncomenda->pesoUnitario = $item_peso;

            }

            print_r($var);

The console output is an array exactly as needed, but with only 1 item, and $invoice->getAllItems()there are 5!

Output from the above array:

2017-12-14T18:17:04+00:00 DEBUG (7): stdClass Object
(
    [xml] => stdClass Object
        (
            [remessa] => stdClass Object
                (
                    [idioma] => PT
                    [encomendas] => stdClass Object
                        (
                            [encomenda] => stdClass Object
                                (
                                    [cartaoDePostagem] => 0057018901
                                    [codigoControleCliente] => 
                                    [nomeRemetente] => 
                                    [enderecoRemetente] => 
                                    [numeroEnderecoRemetente] => 
                                    [complementoEnderecoRemetente] => 
                                    [codigoPostalRemetente] => 
                                    [cidadeRemetente] => 
                                    [estadoRemetente] => 
                                    [paisRemetente] => 
                                    [emailRemetente] => 
                                    [telefoneRemetente] => 
                                    [siteRemetente] => 
                                    [nomeDestinatario] => joao teste
                                    [tipoDocumentoDestinatario] => 1
                                    [numeroDocumentoDestinatario] => 69077886168
                                    [enderecoDestinatario] => addrees test 
                                    [numeroEnderecoDestinatario] => 16
                                    [complementoEnderecoDestinatario] => Sandu Norte
                                    [codigoPostalDestinatario] => 99988009
                                    [caixaPostalDestinatario] => 
                                    [cidadeDestinatario] => Brasília
                                    [ufDestinatario] => DF
                                    [paisDestinatario] => BR
                                    [emailDestinatario] => [email protected]
                                    [telefoneDestinatario] => (61) 9000-0000
                                    [pesoObjeto] => 
                                    [comprimentoObjeto] => 
                                    [larguraObjeto] => 
                                    [alturaObjeto] => 
                                    [modalidadeDistribuicaoObjeto] => 33367
                                    [modalidadePagamentoImpostosObjeto] => 1
                                    [codigoMoeda] => BRL
                                    [valorFreteObjeto] => 
                                    [valorSeguroObjeto] => 
                                    [modoDevolucaoObjeto] => 1
                                    [seguroDevolucaoObjeto] => 1
                                    [itensEncomenda] => stdClass Object
                                        (
                                            [itemEncomenda] => stdClass Object
                                                (
                                                    [codigoSHItem] => 
                                                    [codigoSKUItem] => LI-60669896245-preto
                                                    [descricaoItem] => Smartwatch DZ09 Câmera 2.0 MP Bluetooth Conectividade Redes Sociais Fashion - Preto
                                                    [quantidadeItem] => 2.0000
                                                    [valorUnitario] => 49.9000
                                                    [pesoUnitario] => 0.4000
                                                )

                                        )

                                )

                        )

                )

        )

)

It should be like this (with 5 tags [itemEncomenda]) to mount the xml correctly:

2017-12-14T18:17:04+00:00 DEBUG (7): stdClass Object
(
    [xml] => stdClass Object
        (
            [remessa] => stdClass Object
                (
                    [idioma] => PT
                    [encomendas] => stdClass Object
                        (
                            [encomenda] => stdClass Object
                                (
                                    [cartaoDePostagem] => 0057018901
                                    [codigoControleCliente] => 
                                    [nomeRemetente] => 
                                    [enderecoRemetente] => 
                                    [numeroEnderecoRemetente] => 
                                    [complementoEnderecoRemetente] => 
                                    [codigoPostalRemetente] => 
                                    [cidadeRemetente] => 
                                    [estadoRemetente] => 
                                    [paisRemetente] => 
                                    [emailRemetente] => 
                                    [telefoneRemetente] => 
                                    [siteRemetente] => 
                                    [nomeDestinatario] => joao teste
                                    [tipoDocumentoDestinatario] => 1
                                    [numeroDocumentoDestinatario] => 00000000
                                    [enderecoDestinatario] => address test
                                    [numeroEnderecoDestinatario] => 16
                                    [complementoEnderecoDestinatario] => Sandu Norte
                                    [codigoPostalDestinatario] => 00000000
                                    [caixaPostalDestinatario] => 
                                    [cidadeDestinatario] => Brasília
                                    [ufDestinatario] => DF
                                    [paisDestinatario] => BR
                                    [emailDestinatario] => [email protected]
                                    [telefoneDestinatario] => (61) 9000-0000
                                    [pesoObjeto] => 
                                    [comprimentoObjeto] => 
                                    [larguraObjeto] => 
                                    [alturaObjeto] => 
                                    [modalidadeDistribuicaoObjeto] => 33367
                                    [modalidadePagamentoImpostosObjeto] => 1
                                    [codigoMoeda] => BRL
                                    [valorFreteObjeto] => 
                                    [valorSeguroObjeto] => 
                                    [modoDevolucaoObjeto] => 1
                                    [seguroDevolucaoObjeto] => 1
                                    [itensEncomenda] => stdClass Object
                                        (
                                            [itemEncomenda] => stdClass Object
                                                (
                                                    [codigoSHItem] => 
                                                    [codigoSKUItem] => LI-60669896245-preto
                                                    [descricaoItem] => Smartwatch DZ09 Câmera 2.0 MP Bluetooth Conectividade Redes Sociais Fashion - Preto
                                                    [quantidadeItem] => 2.0000
                                                    [valorUnitario] => 49.9000
                                                    [pesoUnitario] => 0.4000
                                                )
                                            [itemEncomenda] => stdClass Object
                                                (
                                                    [codigoSHItem] => 
                                                    [codigoSKUItem] => LI-60669896245-branco
                                                    [descricaoItem] => Câmera 2.0 MP Bluetooth Conectividade Redes Sociais Fashion - Branco
                                                    [quantidadeItem] => 2.0000
                                                    [valorUnitario] => 49.9000
                                                    [pesoUnitario] => 0.4000
                                                )
                                            [itemEncomenda] => stdClass Object
                                                (
                                                    [codigoSHItem] => 
                                                    [codigoSKUItem] => LI-60669896245-vermelho
                                                    [descricaoItem] => Smartwatch DZ09 Câmera 2.0 MP Bluetooth Conectividade Redes Sociais Fashion - Vermelho
                                                    [quantidadeItem] => 3.0000
                                                    [valorUnitario] => 49.9000
                                                    [pesoUnitario] => 0.4000
                                                )
                                            [itemEncomenda] => stdClass Object
                                                (
                                                    [codigoSHItem] => 
                                                    [codigoSKUItem] => LI-60669896245-verde
                                                    [descricaoItem] => Câmera 2.0 MP Bluetooth Conectividade Redes Sociais Fashion - Verde
                                                    [quantidadeItem] => 1.0000
                                                    [valorUnitario] => 49.9000
                                                    [pesoUnitario] => 0.4000
                                                )
                                            [itemEncomenda] => stdClass Object
                                                (
                                                    [codigoSHItem] => 
                                                    [codigoSKUItem] => LI-60669896245-cinca
                                                    [descricaoItem] => Câmera 2.0 MP Bluetooth Conectividade Redes Sociais Fashion - Cinza
                                                    [quantidadeItem] => 4.0000
                                                    [valorUnitario] => 49.9000
                                                    [pesoUnitario] => 0.4000
                                                )

                                        )

                                )

                        )

                )

        )

)

Maybe this xml should not be mounted with array, because the tag itemEncomenda need to appear several times (according to the number of foreach products) in the same XML, it means (to my mind) that you would have to repeat the array key with the same name, which is not allowed in PHP.

I also tried to create an array as shown below, but stumble on the same problem, array with key with the same name:

$soapArgs = array(
                'xml' => array(
                    'remessa' => array(
                        'idioma' => 'PT',
                        'encomendas' => array(
                            'encomenda' => array(
                                //remetende
                                'cartaoDePostagem' => $this->cartao_postagem,
                                'codigoControleCliente' => '',
                                'nomeRemetente' => $this->remetente_nome,
                                'enderecoRemetente' => $this->remetente_rua,
                                'numeroEnderecoRemetente' => $this->remetente_numero,
                                'complementoEnderecoRemetente' => $this->remetente_complemento,
                                'codigoPostalRemetente' => $this->remetente_cep,
                                'cidadeRemetente' => $this->remetente_cidade,
                                'estadoRemetente' => $this->remetente_estado,
                                'paisRemetente' => $this->remetente_pais,
                                'emailRemetente' => $this->remetente_email,
                                'telefoneRemetente' => $this->remetente_telefone,
                                'siteRemetente' => $this->remetente_site,
                                //destinatário customer
                                'nomeDestinatario' => $customer_nome,
                                'tipoDocumentoDestinatario' => 1, //1-cpf 2-cnpj 3-passport 4-not informed
                                'numeroDocumentoDestinatario' => $customer_taxvat,
                                'enderecoDestinatario' => $customer_rua,
                                'numeroEnderecoDestinatario' => $customer_rua_numero,
                                'complementoEnderecoDestinatario' => $customer_compemento,
                                'codigoPostalDestinatario' => $customer_cep,
                                'caixaPostalDestinatario' => '',
                                'cidadeDestinatario' => $customer_cidade,
                                'ufDestinatario' => $customer_uf,
                                'paisDestinatario' => $customer_pais,
                                'emailDestinatario' => $customer_email,
                                'telefoneDestinatario' => $customer_telefone,
                                //objeto
                                'pesoObjeto' => '',
                                'comprimentoObjeto' => '',
                                'larguraObjeto' => '',
                                'alturaObjeto' => '',
                                'modalidadeDistribuicaoObjeto' => '',
                                'modalidadePagamentoImpostosObjeto' => '',
                                'codigoMoeda' => '',
                                'valorFreteObjeto' => '',
                                'valorSeguroObjeto' => '',
                                'modoDevolucaoObjeto' => '',
                                'seguroDevolucaoObjeto' => '',
                                //itens
                                'itensEncomenda' => array(
                                    'itemEncomenda' => array(
                                        'codigoSHItem' => '',
                                        'codigoSKUItem' => '',
                                        'descricaoItem' => '',
                                        'quantidadeItem' => '',
                                        'valorUnitario' => '',
                                        'pesoUnitario' => ''
                                    ), //itemEncomenda
                                    'itemEncomenda' => array(
                                        'codigoSHItem' => '',
                                        'codigoSKUItem' => '',
                                        'descricaoItem' => '',
                                        'quantidadeItem' => '',
                                        'valorUnitario' => '',
                                        'pesoUnitario' => ''
                                    ), //itemEncomenda
                                    'itemEncomenda' => array(
                                        'codigoSHItem' => '',
                                        'codigoSKUItem' => '',
                                        'descricaoItem' => '',
                                        'quantidadeItem' => '',
                                        'valorUnitario' => '',
                                        'pesoUnitario' => ''
                                    ), //itemEncomenda
                                ), //itensEncomenda
                            ), //encomenda
                        ), //encomendas
                    ), //remessa
                ) //xml
            );

It is perceived that within the array 'itensEncomenda' => array(várias key com itemEncomenda), and this is where I can’t find a solution.

Maybe you should mount XML with XWMWrite() or SimpleXML() but then I come across the problem of not being able to put an XML made in the "hand" inside a Soapvar() to send in the correct pattern that the webservice wsdl waiting.

Can someone shed some light on that?

  • 2

    Hi Junior Maia! Your question was automatically marked as excessively long. If it is possible to shorten and leave only the important parts it increases the possibility for others to have time to read and help.

  • I suggest you remove sensitive information (such as address, name, phone number, document number) and add fictitious information, avoiding the exposure of this information on the network.

1 answer

0

Junior, I don’t know if stdClass is the best approach to generating this XML. I believe that the ideal would be to use a Simplexmlelement object to generate everything. Another approach would be to convert everything into Array. Well, in the case of your code, the problem is "itensEncomenda" needs to be an Array, otherwise you will always overwrite the last registered Dice.

On the line:

$var->xml->remessa->encomendas->encomenda->itensEncomenda = new stdClass;

you must replace by:

$var->xml->remessa->encomendas->encomenda->itensEncomenda = Array();

And within Foreach, it’s best to simplify the code by doing:

...
$itemEncomenda = new stdClass;
$itemEncomenda->codigoSHItem = '';
$itemEncomenda->codigoSKUItem = $item_sku;
$itemEncomenda->descricaoItem = $item_name;
$itemEncomenda->quantidadeItem = $item_qty;
$itemEncomenda->valorUnitario = $item_price;
$itemEncomenda->pesoUnitario = $item_peso;

$var->xml->remessa->encomendas->encomenda->itensEncomenda[] = $itemEncomenda;

This way you create an Array and do not overwrite any of the items. But as I tell you above and I emphasize again, the ideal would be you create the whole structure as Array and at the end convert it to XML for being more flexible this way.

Any questions inform, abs.

  • but if itensEncomenda is array(), it will create numeric index at the positions, where it should be 'itemEncomenda' will be '0' '1'.. And then the wsdl won’t recognize the tag. Should it not be an Arrayobject with Soapvar() ?

Browser other questions tagged

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