Problem to handle XML

Asked

Viewed 364 times

0

I am 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/financial/chk.php on line 42

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

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

follow the path chk

$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>";

}*/
  • Put the chk.php file here and say which is line 42 so we can see the error.

2 answers

2


0

found the solution of the problem look for the commented line

$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);

//in this stop I just took the variable $Sponse and not $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>";

}*/

Browser other questions tagged

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