PHP Nfe 4.0, how to view invoice information?

Asked

Viewed 2,230 times

0

I would like to view Nfe 4.0 on a PHP page, I’ve been studying a little and found several examples, but so far unsuccessful.

index php.

<?php 
$xml= new DOMDocument;
$xml->load( "nfe.xml" );  
if (!$xml) {
echo "Erro ao abrir arquivo!";
exit;
} 
$ver = simplexml_import_dom($xml);

foreach ($ver as $valor) {

echo $valor->detItem->prod->xProd;

}

?>

nfe.xml

<det nItem="1"><prod><cProd>382597</cProd><cEAN>7899882306668</cEAN><xProd>VENT COL MOND NV06 6P 140W BR/AZ 110V</xProd><NCM>84145990</NCM><CEST>2108900</CEST><CFOP>5405</CFOP><uCom>PC</uCom><qCom>1.0000</qCom><vUnCom>189.0000000000</vUnCom><vProd>189.00</vProd>

xProd is product description but nothing appears on the page.

What would be the problem?

  • of the one var_dump($ver) before the foreach to confirm

  • returned ["det"]=> Object(Simplexmlelement)#10 (3) { ["@Attributes"]=> array(1) { ["nItem"]=> string(1) "1" } ["Prod"]=> Object(Simplexmlelement)#15 (15) { ["cProd"]=> string(6) "382597" ["cEAN"]=> string(13) "7899882306668" ["xProd"]=> string(37) "VENT COL MOND NV06 6P 140W BR/AZ 110V" ["NCM"]=> string(8) "84145990" ["CEST"]=> string(7) "2108900" ["CFOP"]=> string(4) "5405" ["uCom"]=> string(2) "PC" ["qCom"]=> string(6) "1.0000" ["vUnWith"]=> string(14) "189.0000000000" ["vProd"]=> string(6) "189.00"

  • 1

    trial echo $valor->det->prod->xProd;, you have to follow the identical nomenclature, you don’t need the foreach in this case, just to test

2 answers

1


Use the function simplexml_load_file (directly already plays the role of the two you are using) and when you want debugar a code do so:

<?php    
    $xml= simplexml_load_file("nfe.xml");   
    if (!$xml) {
        echo "Erro ao abrir arquivo!";
        exit;
    } 
    echo '<pre>';
    print_r($xml);

that your output looks pretty cool to understand how to read each item, note:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [nItem] => 1
        )

    [prod] => SimpleXMLElement Object
        (
            [cProd] => 382597
            [cEAN] => 7899882306668
            [xProd] => VENT COL MOND NV06 6P 140W BR/AZ 110V
            [NCM] => 84145990
            [CEST] => 2108900
            [CFOP] => 5405
            [uCom] => PC
            [qCom] => 1.0000
            [vUnCom] => 189.0000000000
            [vProd] => 189.00
        )    
)

where that instance has a property nItem that can be recovered:

$attributes = $xml->attributes();   
$nItem = strval($attributes['nItem']);

and to retrieve the key prod basically accessing each item:

$children = $xml->children();

$data = array();
$data['cProd'] = (strval($children->prod->cProd));
$data['cEAN'] = (strval($children->prod->cEAN));
$data['xProd'] = (strval($children->prod->xProd));
$data['NCM'] = (strval($children->prod->NCM));
$data['CEST'] = (strval($children->prod->CEST));
$data['CFOP'] = (strval($children->prod->CFOP));
$data['uCom'] = (strval($children->prod->uCom));
$data['qCom'] = (strval($children->prod->qCom));
$data['vUnCom'] = (strval($children->prod->vUnCom));
$data['vProd'] = (strval($children->prod->vProd));

or directly:

$data = ((array)$children->prod);

from that only write echo $data and the nome da chave that you have the valor of each:

echo $data['vProd']; //189.00

and so on.

Ref.

  • 1

    Thank you so much for your help.

  • I have one more question and when you have more items, for example [nItem] => 1, [nItem] => 2, [nItem] => 3, in these cases I have to use foreach ?

  • 1

    @Junior if it is a list, an array always needs to iterate in some way to retrieve the values, so yes it will. It all depends on the format what is in your question is how you read if the format is different, open another query by passing this information

  • Thanks for your help, I’ll follow your recommendations

1

Perhaps it is a small lack of attention, the secret is to remember that it is a multidimensional object, which has several layers containing arrays or other obejtos:

  public 'det' => 
    object(stdClass)[2]
      public 'attributes' => 
        array (size=1)
          'nItem' => string '1' (length=1)
      public 'prod' => 
        object(stdClass)[3]
          public 'cProd' => string '382597' (length=6)
          public 'cEAN' => string '7899882306668' (length=13)
          public 'xProd' => string 'VENT COL MOND NV06 6P 140W BR/AZ 110V' (length=37)
          public 'NCM' => string '84145990' (length=8)
          public 'CEST' => string '2108900' (length=7)
          public 'CFOP' => string '5405' (length=4)
          public 'uCom' => string 'PC' (length=2)
          public 'qCom' => string '1.0000' (length=6)
          public 'vUnCom' => string '189.0000000000' (length=14)
          public 'vProd' => string '189.00' (length=6)

(I reproduced the return you sent in direct object in php, instead of xml, for the explanation, but in your return it will work the same way)

So, using the foreach() just as you were doing, would accuse an error, because there is no object that you were trying to print.

Like the $ver is composed of a object 'det', which in turn consists of a array 'attributes' and another object 'prod', the tie foreach() tries to print from inside the array 'attributes' as if it were an object and accuses an error.

So instead of using the foreach(), in that case, print directly:

echo $ver->det->prod->xPrdo;

Or set a new variable for the root of object 'prod':

$produto=$ver->det->prod;

To facilitate access to information, may be options for you:

echo $produto->xPred;

Edit:

In the case of the file .xml, using the function simplexml_load_file, as quoted by @Virgilionovic in the first reply, the representation does not include the det as in the example I gave, so the impression would be:

//leitura do arquivo
$xml=simplexml_load_file("nfe.xml");
//verifica se o arquivo abriu
if (!$xml) {
    echo "Erro ao abrir arquivo!";
    exit;
}
//imprime os componentes
echo $xml->prod->xProd;

For ease of use, here can also be used a variable for the root:

$produto=$xml->prod;
echo $produto->xPred;
  • 1

    Thank you very much for the explanation

  • @Junior, it worked out?

  • I’m checking because this part I put up is a part of the Invoice, and with the full file, it didn’t work, but based on your response, and Virgil’s response I’m studying it and we’ll see if I can.

Browser other questions tagged

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