Error trying to perform Soap request with SoapClient

0

I'm having trouble making requests to a webservice using PHP's SoapClient (library). The SOAP-ERROR: Encoding: object has been returned in the 'Client' property . Any idea what that might be? Here is the code used.

Class php:     

/**
 * Classe de interação com interface soap
 * 
 * @package     crphp
 * @subpackage  webservice
 * @author      Fábio J L Ferreira <[email protected]>
 * @license     MIT (consulte o arquivo license disponibilizado com este pacote)
 * @copyright   (c) 2016, Fábio J L Ferreira
 */

namespace Crphp\Webservice;

use \Exception;
use \SoapClient;
use \DOMDocument;

class Soap
{
    /**
     * Armazena uma instância de SoapClient
     *
     * @var string 
     */
    private $client;

    /**
     * Consulta o WSDL informado
     * 
     * @param   string       $wsdl
     * @param   array        $opcoes
     * @return  null|string  null = sucesso, string = erro
     */
    public function setWsdl($wsdl, array $opcoes = null)
    {
        if (!$opcoes) {
            $opcoes = [
                'cache_wsdl' => 'WSDL_CACHE_NONE',
                'soap_version' => 'SOAP_1_2',
                'trace' => 1,
                'encoding' => 'UTF-8'
            ];
        }

        try {
            $this->client = new SoapClient($wsdl, $opcoes);
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }

    /**
     * Dispara a consulta contra o serviço informado
     * 
     * @param   string       $acao
     * @param   array        $argumentos
     * @return  null|string  null = sucesso, string = erro
     */
    public function consult($acao, array $argumentos = null)
    {
        try {
            if(!$this->client) {
                throw new Exception("Ocorreu um erro ao tentar chamar o serviço <b>{$acao}</b>.");
            }

            $this->client->__soapCall($acao, array($argumentos));
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }

    /**
     * Retorna os métodos expostos pelo WSDL
     * 
     * @return array|null
     */
    public function getMethods()
    {
        if($this->client) {
            foreach($this->client->__getFunctions() as $metodo) {  
                $array = explode(' ', substr($metodo, 0, strpos($metodo, '(')));
                $metodos[] = end($array);
            }
            return $metodos;
        }
    }

    /**
     * Retorna o XML enviado
     * 
     * @return string|void em caso de sucesso retorna string, para erro retorna null
     */
    public function getRequest()
    {
        return ($this->client) ? $this->client->__getLastRequest() : null;
    }

    /**
     * Retorna o XML recebido
     * 
     * @return string|void em caso de sucesso retorna string, para erro retorna null
     */
    public function getResponse()
    {
        return ($this->client) ? $this->client->__getLastResponse() : null;
    }

    /**
     * Converte string para o formato XML
     * 
     * @param string $soap
     * @return null|string se não tiver dado para transformação retorna null
     */
    public function formatXML($soap)
    {
        if(!$soap) {
            return null;
        }

        $dom = new DOMDocument;
        $dom->preserveWhiteSpace = false;
        $dom->formatOutput = true;
        $dom->loadXML($soap);

        return htmlentities($dom->saveXML());
    }
}

test.php

<?php

require_once('../vendor/autoload.php');

use Crphp\Webservice\Soap;

$args = [
    'ConsultarEnderecoRequest' => [
        'Cliente' => [
            'sistema' => 'xpto',
        ],
        'numDDD'   => 'xx',
        'numTerminal'   => 'xxxxxxxx',
    ]
];

$obj = new Soap;
if($erro = $obj->setWsdl('http://...?wsdl')) {
    exit($erro);
}

// Retorna os métodos expostos pelo WSDL
// $obj->getMethods();

// Se o retorno for null então significa que a consulta não foi realizada
if(!$erro = $obj->consult('Consultar', $args)) {
    // Perfumaria
    echo '<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script>';echo"<pre class='prettyprint' >" . $obj->formatXML($obj->getResponse()) . "</pre>";
} else {
    echo $erro;
}

In a very simple and brief way, the xml submitted should be something like:

<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <soapenv:Body>
  <ConsultarRequest>
   <Cliente>
    <sistema>XPTO</sistema>
   </Cliente>
   <numDDD>xx</numDDD>
   <numTerminal>xxxxxxxx</numTerminal>
  </ConsultarRequest>
 </soapenv:Body>
</soapenv:Envelope>
    
asked by anonymous 03.08.2018 / 20:05

1 answer

0

I have identified the cause of the error. After analyzing the code more calmly, I got to the query (namespace: Crphp \ Webservice \ Soap) method, precisely in this section:

$this->client->__soapCall($acao, array($argumentos));

When I create an instance of this class I'm already passing an array as parameter, so it does not make sense to use:

$this->client->__soapCall($acao, array($argumentos));

but rather:

$this->client->__soapCall($acao, $argumentos);

I have not noticed this error before because in XML without child elements the problem is not noticed.

Correction Commit: link

    
10.08.2018 / 16:18