-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;
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,.
– Ricardo Gomes
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.
– luigibertaco
Does the information of the webservice not correspond to reality? are wrong?
– Ricardo Gomes
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.
– luigibertaco
you have how to edit the question with a different xml return example that may occur?
– luigibertaco
I added a new example to your code
– luigibertaco
Using:$xml = simplexml_load_string($result1[0]); gives the following result:
– Ricardo Gomes
Warning: simplexml_load_string(): Entity: line 1: parser error : Starttag: invalid element name in D:
– Ricardo Gomes
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.– luigibertaco
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
– Ricardo Gomes
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().– luigibertaco