Convert XML to php

Asked

Viewed 974 times

3

I am making a request in php via Curl, and in the reply this returning me an xml, but I can not work with this xml..

REQUISITION:

<br>
curl = curl_init('http://ws.targetmailing.com.br/consulta');<br>
curl_setopt($curl, CURLOPT_HEADER, false);<br>
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);<br>
curl_setopt($curl, CURLOPT_POST, true);<br>
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);<br>
$response = curl_exec($curl);<br>
curl_close($curl);
<br>

RETURN:

 <?xml version="1.0" encoding="UTF-8"?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:resultado xmlns:ns2="http://ws.targetmailing.com.br/consulta" restricao="false" data="2016-04-29T07:42:59.456-03:00"><protocolo numero="1541656894" digito="9"/><operador codigo="1000" nome="INFOMAIL SERVICOS DE BANCO DE DADOS E INFORMATICA"/><consumidor><consumidor-pessoa-juridica data-fundacao="1990-01-01T00:00:00-03:00" nome-comercial="RAZÃO SOCIAL HOMOLOGAÇÃO" razao-social="A EMPRESA LTDA ME"><cnpj numero="74907134000142"/><endereco logradouro="R JOSÉ DA SILVA" numero="10" bairro="CENTRO" cep="01342000"><cidade nome="SÃO PAULO"><estado sigla-uf="SP"/></cidade></endereco></consumidor-pessoa-juridica></consumidor><spc><resumo quantidade-total="0"/></spc></ns2:resultado></S:Body></S:Envelope>

AS I AM TREATING:

<br>
$result = simplexml_load_string($response);

ERROR:

Warning (2): simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found [APP/Controller/EcommerceController.php, line 832]<br>
Warning (2): simplexml_load_string() [function.simplexml-load-string]:  &lt;?xml version=&#039;1.0&#039; encoding=&#039;UTF-8&#039;?&gt;&lt;S:Envelope  [APP/Controller/EcommerceController.php, line 832]<br>
Warning (2): simplexml_load_string() [function.simplexml-load-string]:  ^ [APP/Controller/EcommerceController.php, line 832]
  • I tested your XML here and it worked ok. Some configuration in your php is removing the tag <? ?> XML. .

  • Maycon test the two examples separately please: http://answall.com/a/139663/3635 and tell me if both worked, otherwise tell me which one failed.

2 answers

1

The problem is clearly in Webservice, outside that address /consulta if open directly emits a series of errors, which makes me think that this WS is with several flaws, however if it is not yours there is no way to solve then.

What happens is that it is generating entities in place of several characters, for example &lt; in place of <.

Provisionally use html_entity_decode to correct:

curl = curl_init('http://ws.targetmailing.com.br/consulta');
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($curl);
curl_close($curl);

$response = html_entity_decode($response);

$result = simplexml_load_string($response);

However I recommend you contact the responsible for WS and check if you have to pass any header, for example:

curl = curl_init('http://ws.targetmailing.com.br/consulta');
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Accept: application/xml,text/xml;q=0.9,text/plain;q=0.8'
));

$response = curl_exec($curl);

curl_close($curl);

$result = simplexml_load_string($response);

0

In PHP, in such cases, it is very common for you to use @ in these functions to suppress errors. Some case, it is very common this happens with DomDocument.

So you can do it this way:

$dom = new DomDocument();  

@$dom->loadXml($result_curl);

Or else

@simplexml_load_string($result_curl)

Note: In the code xml posted on the question has a space before the <?xml ?>, I did the tests necessary to realize that this would affect the parser of xml of PHP. If that’s your case, you can also do so:

 simplexml_load_string(ltrim($result_curl));

Observe these two tests:

https://ideone.com/H7PbMJ

  • Ne question I missed, is this returning me like this: &lt;? xml version=&#039;1.0&#039; encoding=&#039;UTF-8&#039;? &gt;&lt;S:Envelope xmlns:S=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;&gt;&lt;S:Body&gt;&lt;S:Fault xmlns:ns4=&quot;http://www.w3.org/2003/05/soap-envelope&quot;&gt;&lt;faultcode&gt;S:Client&lt;/faultcode&gt;&lt;faultstring&gt;org.xml.sax.SAXParseException; cvc-enumeration-Valid: Value &#039;&#039; is not Facet-Valid with respect to enumeration &#039;[F, J]&#039;. It must be a value from the enumeration. &lt;/faultstring&gt;&lt;/S:Fault&gt;&lt;/S:Body&gt;&lt;/S:Envelope&gt;

Browser other questions tagged

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