Convert XML into php [closed]

3

I am making a request in php via curl, and the answer is returning an xml, however I can not work with this xml ..

REQUIREMENT:

<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>

HOW 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]
    
asked by anonymous 07.07.2016 / 19:57

2 answers

1

The problem is clearly in the WebService, except that the address /consulta if opened directly emits a series of errors, which makes me think that this WS has several faults, however if it is not yours it can not solve so.

What happens is that it is generating entities instead of multiple characters, such as &lt; instead of < .

Temporarily use html_entity_decode to fix:

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 that you contact the person responsible for WS and check if you have to spend some 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);
    
08.07.2016 / 21:34
0

In PHP, in these cases, it is quite common for you to use @ in these functions to suppress errors. In some cases, this is very common with DomDocument .

So you can do it this way:

$dom = new DomDocument();  

@$dom->loadXml($result_curl);

Or

@simplexml_load_string($result_curl)

Note : In the code xml posted in the question has a space before <?xml ?> , I made the tests necessary to realize that this would affect the xml parser of PHP. If this is the case, you can also do this:

 simplexml_load_string(ltrim($result_curl));

Look at these two tests:

link

    
08.07.2016 / 20:59