Connection with WebService SOAP WSDL with XML return

6

I have the following code that works perfectly. The webservice return is XML , but the return I'm getting is a string. What should I do to get XML as a return and not a string?

$client = new SoapClient('http://www.roveri.inf.br/ws/cnpj.php?wsdl');

$function = 'getCNPJ';

$arguments= array(
    'token' => $token,
    'cnpj'  => $cnpj
);

$options = array('location' => 'http://www.roveri.inf.br/ws/cnpj.php');


$result = $client->__soapCall($function, $arguments, $options);

echo 'Response: ';

print_r($result);
    
asked by anonymous 30.01.2017 / 13:23

1 answer

6

You can use simplexml_load_string to convert your xml string to a SimpleXMLElement object.

Example:

<?php
$xmlstring=
"<?xml version='1.0' encoding='UTF-8'?>
<teste>
    <pessoa>
        <nome>Alan</nome>
        <profissao>Desenvolvedor Web</profissao>
    </pessoa>
    <pessoa>
        <nome>Alexandre</nome>
        <profissao>Analista de Sistemas</profissao>
    </pessoa>   
</teste>";

$xml=simplexml_load_string($xmlstring) or die("Erro");

var_dump($xml);

foreach($xml as $pessoa)
{
    echo $pessoa->nome."<br>";
}


?> 
    
30.01.2017 / 14:02