json_encode returning TAGS HTML [closed]

-1

When performing echo json_encode($data); , I am receiving, via console.debug(data) , the following:

<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined variable: v_prod_total in C:\wamp64\www\nfe\admin\nfephp-master\exemplos\NFe.00testaMakeNFe.php on line <i>464</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0020</td><td bgcolor='#eeeeec' align='right'>424896</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\nfe\admin\nfephp-master\exemplos\NFe.00testaMakeNFe.php' bgcolor='#eeeeec'>....00testaMakeNFe.php<b>:

What could be happening?

Script:

$http({
          method  : 'POST',
          url     : 'enviar.php',
          dataType: 'json',
          data : dados: dados ,
          headers : {'Content-Type': 'application/x-www-form-urlencoded'}
         })
         .success(function(data) {
            if (data.errors) {
              $scope.erroValidar = "Erro";
            } else {
              $scope.key = data.chave;
              console.debug(data);
            }
          })

Note: json_encode is between an "infinity" of code in PHP     

try{
        $query = $conecta->prepare('SELECT * FROM empresas WHERE situacao=1');
        $query->execute();
        $resultado = $query->fetchAll(PDO::FETCH_ASSOC);
    }catch (PDOexception $erro){
        echo 'Erro ao selecionar: '.$erro->getMessage();
    }

    foreach($resultado as $res){
        $razao_social_emt   = $res['cidade'];
        $nome_fantasia_emt  = $res['nome_fantasia'];
        $cnpj_emt           = $res['cnpj'];
        $insc_estadual_emt  = $res['insc_estadual'];
        $logradouro_emt     = $res['logradouro'];
        $numero_emt         = $res['numero'];
.
.
.
echo json_encode($data);
.
.
.
else {
    header('Content-type: text/html; charset=UTF-8');
    foreach ($nfe->erros as $err) {
        echo 'tag: &lt;'.$err['tag'].'&gt; ---- '.$err['desc'].'<br>';
    }
}
?>
    
asked by anonymous 09.01.2017 / 18:21

4 answers

2

Your code is returning an error message:

The variable v_prod_total is not defined.

In the file: C: \ wamp64 \ www \ nfe \ admin \ nfephp-master \ examples \ NFe \ 4.00testaMakeNFe.php

Line: 464

Beyond this, check if there are any other undefined variables.

If they are numeric and looped, place their values before the loop with the initial value equal to ZERO.

    
09.01.2017 / 18:25
2

Let me translate what happened:

1 - This HTML there with this class x-debug is not strange to me. You're probably using the Xdebug extension in PHP.

2 - This HTML only appears when it has an error.

3 - There is an error. It is Notice: Undefined variable: v_prod_total . You are trying to access a variable that has not been defined.

4 - The problem is not a json_encode function. If you resolve the error, the return JSON will be correct (if you do not have another error, of course).

    
09.01.2017 / 18:59
1

I use the following headers to return JSON:

<?php

// No topo da página
header("Access-Control-Allow-Orgin: *"); // CORS
header("Access-Control-Allow-Methods: *"); // Verbos HTTP[S]: PATCH, PUT, DELETE, GET, POST
header("Content-Type: application/json");

Another thing is that your code is reading the condition passed in the IF and returning what is in ELSE, which contains a header that returns HTML. This HTML is simply formatting the error message generated by line 464 (4.00testaMakeNFe.php).

You may generate the error in another way:

.
.
.
else {
    // Remove o cabeçalho
    foreach ($nfe->erros as $err) {
        errors[] = $err;
    }
    echo json_encode($err, JSON_NUMERIC_CHECK);
}
    
09.01.2017 / 18:56
0

With the help of @AllanAndrade, via chat, we were able to find that the problem was in variáveis não inicializadas and chamada de outras funções/bibliotecas que estavam utilizando print_r, var_dump e echo .

    
10.01.2017 / 03:56