Webservice SOAP with PHP

Asked

Viewed 2,654 times

-1

I have one question: I’ve used webservice Json Curl with PHP and in consumption returned me a array where I could work with the information, now learning SOAP the return is not satisfactory as in JSON, the most I get is an XML return in a single line. Does anyone have any tips? Follow 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" only the data as in JSON:

Array->nome;
   Array->cartao;

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 it is already possible to use $xml->body and any other elements.

In your case, I believe it would be something like

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

And I could use

$xml->G_ServApp_Response->consulta_disponivel->nome

Or something like.

Source: PHP.net simplexml_load_string

  • I already tried this procedure,but I have to type the xml, as PHP use I tried to put it in a variable because it may be that the information is different to each search,.

  • As it is in the example the object is created dynamically according to the XML structure. The same way it would be in the case of the Json you commented on. From the generated object your application should be prepared to handle all possible returns.

  • Does the information of the webservice not correspond to reality? are wrong?

  • Even if a web service error comes back, this should be predicted by your code. either when converting XML to Object, or when checking the return http header.

  • you have how to edit the question with a different xml return example that may occur?

  • I added a new example to your code

  • Using:$xml = simplexml_load_string($result1[0]); gives the following result:

  • Warning: simplexml_load_string(): Entity: line 1: parser error : Starttag: invalid element name in D:

  • Try to give a echo $result1[0] to be really is only returning XML or if it is inside some other structure, you must pass to the function simplexml_load_string only texo/xml pure.

  • Giving an echo $result1[0] ---Warning: simplexml_load_string(): Entity: line 1: parser error : Starttag: invalid element name in D:192 Warning: simplexml_load_string(): < in

  • Seeing your answer below, try to use $xml = simplexml_load_string($result1); (without using the array position) I believe it works. About the echo you posted in the last comment, it was to echo only the result ($result1[0]) and not in the simplexml_load_string function().

Show 6 more comments

1

Guys I found a solution(but I think half gambiarra!!!). I took 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 blew up:

$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 didn’t feel firm about using the __getLastResponse, I realize that it is half cat and I noticed that wsdl has no separator.

  • The answers were left with >0</ I can’t even take with str_replace

Browser other questions tagged

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