PHP code example of platinum-place / php-dgii-xml-signer

1. Go to this page and download the library: Download platinum-place/php-dgii-xml-signer 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/ */

    

platinum-place / php-dgii-xml-signer example snippets


   // Cambiar de:
   $canonicalData = $element->C14N(true, false);
   // A:
   $canonicalData = $element->C14N(false, false);
   

   // Cambiar de:
   $c14nSignedInfo = $signedInfoElement->C14N(true, false);
   // A:
   $c14nSignedInfo = $signedInfoElement->C14N();
   

use PlatinumPlace\DgiiXmlSigner\SignManager;

$signer = new SignManager();

// Leer el certificado
$certContent = file_get_contents('path/to/certificate.p12');
$password = 'tu_password';

// XML a firmar
$xml = '<root>...</root>';

// Firmar XML
$signedXml = $signer->sign($certContent, $password, $xml);
file_put_contents('signed_invoice.xml', $signedXml);

// Verificar Firma XML
try {
    $signer->verifyXmlSignature($signedXml);
    echo "¡La firma digital del XML es válida!";
} catch (\Selective\XmlDSig\Exception\XmlSignatureValidatorException $e) {
    echo "Firma inválida o error de verificación: " . $e->getMessage();
}