PHP code example of xmlvalid / xmlvalid-php

1. Go to this page and download the library: Download xmlvalid/xmlvalid-php 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/ */

    

xmlvalid / xmlvalid-php example snippets


use Xmlvalid\Client;
use Xmlvalid\Document;

$xmlvalid = new Client(getenv('XMLVALID_API_KEY'));

$validation = $xmlvalid->validate(
    Document::file('invoice.xml'),
    schema: 'peppol-bis-3-invoice',
);

if ($validation->isValid()) {
    echo "Valid\n";
} else {
    foreach ($validation->errors() as $error) {
        echo $error, PHP_EOL; // Line 139: [BR-CO-16] Amount due for payment (BT-115) = …
    }
}

Document::file('invoice.xml');                       // a file (XML, .gz, .zip, EPUB, Factur-X PDF)
Document::string($xml, 'invoice.xml');               // a string
Document::stream($resource, 'invoice.xml');          // an open stream
Document::url('https://example.com/feed.xml');       // a public URL, downloaded by xmlvalid

$validation->status;               // done, failed (on xmlvalid's side) or cancelled
$validation->result->outcome;      // valid, warnings, invalid, schema, input or failed
$validation->result->errorsTotal;  // all of them; the first 500 are listed
foreach ($validation->errors() as $error) {
    $error->line; $error->column; $error->level; $error->type; // xsd, schematron, …
    $error->rule;                  // BR-CO-16 for a business rule
    $error->path;                  // where it fired: /Invoice/cac:LegalMonetaryTotal
    $error->message;
    $error->ruleUrl;               // the rule's page on xmlvalid.com
    $error->helpUrl;               // how to fix this kind of error
}
$validation->warnings();
foreach ($validation->ruleSets as $set) {  // the business rules run, at the releases used
    echo $set;                     // Peppol BIS Billing 3.0 rules 3.0.21; $set->pin() for rulesVersion
}
$validation->result->sha256;       // SHA-256 of the file as sent
$validation->result->engine;       // Engine: xmlvalid 0.1.0, libxml2 2.13.9, saxonjs 2.7.0 (null without rules)
$validation->raw;                  // the API's JSON, as an array

$written = $xmlvalid->createInvoice([
    'profile' => 'xrechnung',
    'number' => 'RE-2026-0042',
    'buyerReference' => '04011000-12345-03',
    'seller' => ['name' => 'Muster Software GmbH', 'street' => 'Hauptstraße 12', 'city' => 'Berlin', 'postcode' => '10115', 'country' => 'DE',
        'vatId' => 'DE136695976', 'contact' => ['name' => 'Anna Schmidt', 'phone' => '+49 30 1234567', 'email' => '[email protected]']],
    'buyer' => ['name' => 'Beispiel Handel AG', 'street' => 'Marktplatz 3', 'city' => 'München', 'postcode' => '80331', 'country' => 'DE',
        'endpoint' => ['scheme' => 'EM', 'id' => '[email protected]']],
    'payment' => ['iban' => 'DE02120300000000202051'],
    'lines' => [['name' => 'Softwarelizenz', 'quantity' => 1, 'unit' => 'ANN', 'price' => 1200, 'rate' => 19]],
]);

$written->isValid();                       // checked against the profile's rules
$written->invoice['totals'];               // ['net' => …, 'tax' => …, 'payable' => …] as written
$written->invoice['ignored'];              // fields sent that an invoice has no place for
$xml = $xmlvalid->invoiceDocument($written->id);  // the XML (or PDF), kept for 24 hours

$validation = $xmlvalid->submit(Document::file('big.xml'), schema: 'ubl-2.1-invoice');
// …
$validation = $xmlvalid->waitFor($validation->id);   // or $xmlvalid->validation($id), $xmlvalid->cancel($id)

$html = $xmlvalid->report($validation->id);          // the HTML report
$link = $xmlvalid->share([$validation->id]);         // a public link for 30 days (Pro): $link->url

$usage = $xmlvalid->usage();                         // $usage->used, $usage->limit, $usage->remaining()
$schemas = $xmlvalid->schemas();                     // your saved schemas, then the catalog

use Xmlvalid\Model\Event;
use Xmlvalid\Webhook;

$event = Webhook::verify(
    file_get_contents('php://input'),
    $_SERVER['HTTP_XMLVALID_SIGNATURE'] ?? '',
    getenv('XMLVALID_WEBHOOK_SECRET'),
); // throws SignatureVerificationException

if ($event->type === Event::VALIDATION_COMPLETED) {
    $validation = $event->validation();
}

new Client(
    apiKey: 'xv_…',
    baseUrl: 'https://xmlvalid.com/api/v1',
    httpClient: $psr18Client,        // with requestFactory: and streamFactory: (PSR-17)
    timeout: 900,                    // seconds validate() waits in all
    wait: 10,                        // seconds the API holds a new request (0-60)
    maxRetries: 3,                   // retries of a 429 rate_limited
);
bash
composer