How to separate string that Soap generates?

Asked

Viewed 552 times

0

if(!empty($_POST['submit'])) {
$client = new SoapClient('http://hidroweb.ana.gov.br/fcthservices/mma.asmx?WSDL');

$function = 'Estado';

$options = array('location' => 'http://hidroweb.ana.gov.br/fcthservices/mma.asmx');

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

var_dump($result);
?>

The result is this:

Object(stdClass)#2 (1) { ["Estadoresult"]=> Object(stdClass)#3 (1) { ["any"]=> string(2956) "URURUGUAIARARGENTINAPGPARAGUAICHCHILEBOBOLÍVIAPUPERUCOCOLÔMBIAEQEQUADORVEVENEZUELAGUGUIANASUSURINAMEGFGUIANA FRANCESA11RORONDÔNIA12ACACRE13AMAMAZONAS14RRRORAIMA15PAPARÁ16APAMAPÁ17TOTOCANTINS21MAMARANHÃO22PIPIAUÍ23CECEARÁ24RNRIO GREAT OF NORTE25PBPARABA26PEPERNAMBUCO27ALAGOAS28SESERGIPE29BABAHIA31MGMINAS GERAIS32ESESPING PAULO41PRAPRANA42SCSANTA CATARINA43RSRIO GRANDE DO SUL50MSMATATO GROSSO OF THE FEDERAL POLICE" } }

I don’t understand I need the object to come apart. Because Soap is returning everything together. It’s like??

  • Exactly what is returning all together? The string inside the any or the object as a whole?

  • Returns that ["any"]=> string(2956) all together! But the xml is separated. Take a look here. http://hidroweb.ana.gov.br/fcthservices/mma.asmx/BaciaDataSet Deveria vim with the XML structure. The following: Diffgr basin:id="Bacia1" msdata:rowOrder="0"> <Code>1</Code> <Name>AMAZON RIVER</Name> </Basin>

  • It was to return a structured object with Basin > code > name. Got it?

1 answer

2


You don’t need to separate anything, the return is probably in order and separate.

The problem is just the way you are displaying the data on the screen.

You can see that even your string between quotes has about 480 characters, and the indicated return was 2956 characters. The returned tags are not being shown on the screen, because the browser try to interpret them.

Test like this:

if(!empty($_POST['submit'])) {
$client = new SoapClient('http://hidroweb.ana.gov.br/fcthservices/mma.asmx?WSDL');
$function = 'Estado';
$options = array('location' => 'http://hidroweb.ana.gov.br/fcthservices/mma.asmx');
$result = $client->__soapCall($function,$options);

//VISUALIZAÇÃO EM HTML

// Vamos capturar a saída...
ob_start();
var_dump($result);
$saida = ob_get_contents();
ob_end_clean();

//e formatar para ver na tela:
echo nl2br( htmlentities( $saida ) );

Note: depending on the case it is best to use print_r in place of var_dump.

To convert XML into an object, we already have a response on the site:
Webservice SOAP with PHP

  • Very good. But can you take the xml tags??? Leave only the data?? Example: ["Acronym"]=> "UF", ["State"] => "São Paulo"....

  • What I really wanted was for him not to return everything within the ["any"], but rather every structured value. Example again: ["Acronym"]=> "UF", ["State"] => "São Paulo"

  • Because that way I’ll be able to manipulate the values of the object in an easy way.

  • Then just a little search on the site itself. See a functional example that I found immediately after your comment, simply searching for XML and PHP: http://answall.com/a/33806/70 - so XML becomes an object, and you can individually access members.

  • Excuse my ignorance, but I don’t have much experience with webservices :( //and format to see on screen: $saida = nl2br( htmlentities( $saida ) ); $xml = simplexml_load_string($saida->Estadoresult->any); ;????

  • the load string is in $result, the part of the output you can delete completely. I just showed you how to visualize on the screen. The part I put //VISUALIZAR henceforth you can delete everything in the definitive code, after you check that everything is ok.

  • I put it like this and it worked!!! $xml = simplexml_load_string($result->Statessult->any);

  • Thanks a million times!!! You’re a beast!!!!

Show 3 more comments

Browser other questions tagged

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