Problem handling XML

0

I'm trying the following error when trying to manipulate an XML string

This is the XML I'm trying to read:

<?xml version="1.0" encoding="utf-8"?>
<integracao>
    <status>2</status>
    <resposta>
        <paginacao>
            <totalItens>2</totalItens>
            <paginaAtual>1</paginaAtual>              
            <registrosPorPagina>10</registrosPorPagina>        
            <ultimaPagina>1</ultimaPagina>
        </paginacao>
        <historico>
            <registro>
                <transacao>19569951</transacao>
                <email></email>
                <valor>2000</valor>        
                <dataEmissao>2014-09-02T12:09:14</dataEmissao>       
                <status>aguardando</status>
                <codigoStatus>1</codigoStatus>
            </registro>
            <registro>
                <transacao>19561474</transacao>
                <email></email>
                <valor>2000</valor> 
                <dataEmissao>2014-09-01T01:09:20</dataEmissao>  
                <status>aguardando</status>
                <codigoStatus>1</codigoStatus>
            </registro>
        </historico>
    </resposta>
</integracao>

This is the error that appears

  

Warning: simplexml_load_string (): Entity: line 1: parser error: XML   declaration allowed only at the start of the document in   /var/www/gweb/admin/financeiro/chk.php on line 42

     

Warning: simplexml_load_string (): in /var/www/gweb/admin/financeiro/chk.php on line   42

     

Warning: simplexml_load_string (): ^ in   /var/www/gweb/admin/financeiro/chk.php on line 42

follows the chk file

$url = "https://go.gerencianet.com.br/api/historico/xml";
$token = "";

$xml = "<?xml version='1.0' encoding='utf-8'?>
        <integracao>
            <dataInicial>2013-01-01</dataInicial>
            <dataFinal>2014-12-31</dataFinal>
            <registrosPorPagina>10</registrosPorPagina>
            <pagina>1</pagina>
        </integracao>";

$xml = str_replace(array("\n", "\r", "\t"), '', $xml);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
$data = array("token" => $token, "dados" => $xml);

curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
$response = curl_exec($ch);

curl_close($ch);

  echo $xml2 = "<xmp>".$response."</xmp>";

    echo '<br />';




#define o encoding do cabeçalho para utf-8
@header('Content-Type: text/html; charset=utf-8');
#carrega o arquivo XML e retornando um Objeto
$xml = simplexml_load_string("$xml2");

/*
foreach($xml->livro as $livro)
{
 echo $livro->cod;
#usando o utf8_decode para exibir com acentos
 echo $livro->titulo;
echo $livro->autor;
echo $livro->descricao;
echo $livro->preco;
echo "<br>";

}*/
    
asked by anonymous 04.09.2014 / 17:12

2 answers

2

This is a common error when having comments or whitespaces at the beginning of your XML. Make sure there is something of the sort in your document.

There are two links that point to more robust solutions:

Simplexml_load_file: XML

declaration allowed only at the start of the document !

Problem - XML declaration allowed only at the start of the document !

    
04.09.2014 / 18:15
0

I found the problem solution look for the commented line

$ url=" link ";     $ token="";

$xml = "<?xml version='1.0' encoding='utf-8'?>
        <integracao>
            <dataInicial>2013-01-01</dataInicial>
            <dataFinal>2014-12-31</dataFinal>
            <registrosPorPagina>10</registrosPorPagina>
            <pagina>1</pagina>
        </integracao>";

$xml = str_replace(array("\n", "\r", "\t"), '', $xml);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
$data = array("token" => $token, "dados" => $xml);

curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
$response = curl_exec($ch);

curl_close($ch);

// in this stop I just got the $ response variable and not the $ xml2

  echo $xml2 = "<xmp>".$response."</xmp>";

    echo '<br />';




#define o encoding do cabeçalho para utf-8
@header('Content-Type: text/html; charset=utf-8');
#carrega o arquivo XML e retornando um Objeto

///apenas troque a variavel nesta linha e tudo funciono perfeitamente

$xml = simplexml_load_string("$response");

/*
foreach($xml->livro as $livro)
{
 echo $livro->cod;
#usando o utf8_decode para exibir com acentos
 echo $livro->titulo;
echo $livro->autor;
echo $livro->descricao;
echo $livro->preco;
echo "<br>";

}*/
    
04.09.2014 / 19:26