PHP code example of controlaltjeff / p7m-fattura-decoder

1. Go to this page and download the library: Download controlaltjeff/p7m-fattura-decoder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

controlaltjeff / p7m-fattura-decoder example snippets


use Controlaltjeff\P7MDecoder\P7MDecoder;

// Decodifica da file
$xml = P7MDecoder::decodeFile('fattura.xml.p7m');

// Decodifica da stringa
$xml = P7MDecoder::decodeString($p7mContent);

if ($xml === null) {
    throw new \RuntimeException('Impossibile decodificare il file P7M');
}

// Correzioni automatiche applicate durante la decodifica
$corrections = P7MDecoder::getLastCorrections();
foreach ($corrections as $fix) {
    echo "Tag corretto: {$fix['from']} → {$fix['to']}\n";
}

$xml = P7MDecoder::decodeFile('/percorso/della/fattura.p7m');

$content = file_get_contents('fattura.p7m');
$xml = P7MDecoder::decodeString($content);

// Dopo una chiamata a decodeFile/decodeString
$corrections = P7MDecoder::getLastCorrections();
// Restituisce: [['from' => 'DettHaglioLinee', 'to' => 'DettaglioLinee', 'distance' => 1], ...]

use Controlaltjeff\P7MDecoder\P7MDecoder;

// Decodifica un file FatturaPA .p7m ricevuto dal SDI
$xml = P7MDecoder::decodeFile('IT01234567890_FPR12_001.xml.p7m');

if ($xml === null) {
    die('Decodifica fallita');
}

// Analizza l'XML
$dom = new DOMDocument();
$dom->loadXML($xml);

// Accedi ai dati della fattura
$xpath = new DOMXPath($dom);
$numeroFattura = $xpath->query('//Numero')->item(0)->nodeValue;
$importoTotale = $xpath->query('//ImportoTotaleDocumento')->item(0)->nodeValue;

use Controlaltjeff\P7MDecoder\P7MDecoder;

$files = glob('/percorso/delle/fatture/*.p7m');
foreach ($files as $file) {
    $xml = P7MDecoder::decodeFile($file);
    if ($xml !== null) {
        file_put_contents(str_replace('.p7m', '.xml', $file), $xml);
        echo "Decodificato: " . basename($file) . "\n";
    } else {
        echo "Fallito: " . basename($file) . "\n";
    }
}

use Controlaltjeff\P7MDecoder\P7MDecoder;

$xml = P7MDecoder::decodeFile('fattura-corrotta.p7m');
$corrections = P7MDecoder::getLastCorrections();

if (!empty($corrections)) {
    echo count($corrections) . " correzioni automatiche applicate:\n";
    foreach ($corrections as $fix) {
        echo "  {$fix['from']} → {$fix['to']}\n";
    }
}