Connection with Webservice SOAP WSDL with XML return

Asked

Viewed 1,391 times

6

I have the following code that works perfectly. The return of the webservice is a XML, but the return I’m getting is a string. What should I do to receive the XML as 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);

1 answer

6


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>";
}


?> 

Browser other questions tagged

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