Webservice SOAP with PHP

0

I have a question: I've used webservice Json Curl with PHP and in consumption I was returning an array where I could work with the information, now learning SOAP the return is not as satisfying as in JSON, max that I get is an XML return on a single line. Anyone have any tips? Here's my script:

$client = new SoapClient("https://minhaurl.com.br?wsdl",
              array('cache_wsdl' => WSDL_CACHE_NONE,'trace' => true,"encoding" => "utf-8","features" => SOAP_SINGLE_ELEMENT_ARRAYS,"exceptions" => true));
       $param = array(
                       'versao'      => '3.0',
                       'cod_input'   => 'C',
                       'cartao'      => 'xxxxxxxxxxx', 
                       'proxy'       => 'x',
                       'usr'         => 'xxxxxx',
                       'pwd'         => 'jS1_Njg2b8b0WMbU' );

        //nome do método
       $resultado = $client->consulta_disponivel($param);
       echo "<pre>\n";
       print_r ($resultado);
       echo "</pre>\n";   
     //Aqui obtenho o retorno:

    stdClass Object
               (
               [return] => LUIZ ALBERTO43326200004787200000000
               )
        //Usando: 
        $result1 = ( $client->__getLastResponse());
         var_dump($result1 );
        //Obtenho:
               array(1) {
                   [0]=>
                   string(701) "<G_ServApp_Response><consulta_disponivel><nome>LUIZ ALBERTO</nome>                                      <cartao>4332620000478720</cartao><proxy>0</proxy><limite_credito>0</limite_credito><disponivel_saques>0</disponivel_saques><disponivel_compras>0</disponivel_compras><saldo_atual>0</saldo_atual></consulta_disponivel><codigo_retorno>00</codigo_retorno></G_ServApp_Response>"
                  }

How can I "capture" just the data as in JSON:

Array->nome;
   Array->cartao;
    
asked by anonymous 24.09.2014 / 20:53

2 answers

1

You can convert the XML return to an object with the function simplexml_load_string

<?php
$string = <<<XML
<?xml version='1.0'?> 
<document>
 <title>Forty What?</title>
 <from>Joe</from>
 <to>Jane</to>
 <body>
  I know that's the answer -- but what's the question?
 </body>
</document>
XML;

$xml = simplexml_load_string($string);

print_r($xml);
?>

The above example will print:

SimpleXMLElement Object
(
  [title] => Forty What?
  [from] => Joe
  [to] => Jane
  [body] =>
   I know that's the answer -- but what's the question?
)

At this point you can already use $ xml-> body and any other elements.

In your case, I think it would be something like

$xml = simplexml_load_string($result1[0]);

And could use

$xml->G_ServApp_Response->consulta_disponivel->nome

Or something like that.

Source: PHP.net simplexml_load_string

    
25.09.2014 / 01:31
1

Personal I found a solution (but I think half gambiarra !!!). Got it:

    $result1 = ( $client->__getLastResponse());
         var_dump($result1 );
        //Obtenho:
               array(1) {
                   [0]=>
                   string(701) "<G_ServApp_Response><consulta_disponivel><nome>LUIZ ALBERTO</nome>                                      <cartao>4332620000478720</cartao><proxy>0</proxy><limite_credito>0</limite_credito><disponivel_saques>0</disponivel_saques><disponivel_compras>0</disponivel_compras><saldo_atual>0</saldo_atual></consulta_disponivel><codigo_retorno>00</codigo_retorno></G_ServApp_Response>"
                  }

I made an explode:

$nome_arquivo = $result1;
       $arquivo = explode('nome', $nome_arquivo);
       print ("Portador" . $arquivo[1].'<br>');
       $arquivo = explode('cartao', $nome_arquivo);
       print ("Cartão" . $arquivo[1].'<br>');
       $arquivo = explode('proxy', $nome_arquivo);
       print ("Proxy" . $arquivo[1].'<br>');
       $arquivo = explode('limite_credito', $nome_arquivo);
       print ("Limite" . $arquivo[1].'<br>');
       $arquivo = explode('disponivel_saques', $nome_arquivo);
       print ("disponivel_saques" . $arquivo[1].'<br>');
       $arquivo = explode('disponivel_compras', $nome_arquivo);
       print ("disponivel_compras" . $arquivo[1].'<br>');
       $arquivo = explode('saldo_atual', $nome_arquivo);
       print ("saldo_atual" . $arquivo[1].'<br>');
       $arquivo = explode('codigo_retorno', $nome_arquivo);
       print ("codigo_retorno" . $arquivo[1].'<br>');

I got the answers:

Portador>LUIZ ALBERTO</

Cartão>4332620000478720</

Proxy>0</

Limite>0</

disponivel_saques>0</

disponivel_compras>0</

saldo_atual>0</

codigo_retorno>00</

I just did not feel strongly about using __getLastResponse , I realize it's kind of a cat and I noticed that wsdl has no separator.

    
25.09.2014 / 17:38