PHP code example of attestwire / attestwire-php

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

    

attestwire / attestwire-php example snippets


use Attestwire\Client;

$client = new Client($_ENV['ATTESTWIRE_API_KEY']);
$result = $client->validate(file_get_contents('invoice.xml'));

if (!$result->valid) {
    foreach ($result->errors() as $finding) {
        echo "{$finding->rule}: {$finding->message}\n";
        echo "  fix: {$finding->fix}\n";
    }
}

final class ValidationResult
{
    public readonly bool $valid;
    /** @var list<Finding> */
    public readonly array $findings;     // errors + warnings + information, in that order
    public readonly ?string $profile;
    public readonly ?string $syntax;     // "ubl" or "cii", when the document said which
    public readonly ?string $container;  // the PDF attachment name, for a Factur-X/ZUGFeRD PDF
    public readonly ?string $source;     // what was checked, and what it does not prove
    public readonly ?array $raw;         // the full decoded JSON, for anything not modelled above

    public function errors(): array;        // severity === "fatal" — these set valid = false
    public function warnings(): array;      // severity === "warning"
    public function information(): array;   // severity === "information" (advisory)
}

final class Finding
{
    public readonly ?string $rule;              // "BR-DE-15", "ATW-CREDIT-NOTE-...", or an "AW-*" whole-document finding
    public readonly ?string $severity;          // "fatal" | "warning" | "information"
    public readonly ?string $message;
    public readonly ?string $fix;
    public readonly string|array|null $field;   // the business term(s), e.g. "BT-10" or ["BT-31", "BT-32"]
    public readonly ?string $xpath;
    public readonly ?string $docsUrl;           // https://attestwire.com/rules/<rule>, when the finding is a rule
    public readonly ?string $example;
    public readonly ?Location $location;        // line/column/path in the document you sent, when applicable

    public function fieldAsString(): string;    // $field joined with ", "; "" when there is none
    public function isFatal(): bool;
    public function isWarning(): bool;
    public function isInformation(): bool;
}

final class Location
{
    public readonly ?int $line;
    public readonly ?int $column;
    public readonly ?string $path;
    public readonly ?bool $exact;
    public readonly ?string $attachment;   // set only for a PDF: which embedded XML this is in
}

new Client(
    apiKey: 'aw_live_...',                       // or omit and set ATTESTWIRE_API_KEY
    httpClient: null,                              // your own Attestwire\Http\HttpClientInterface; defaults to CurlHttpClient
    origin: Client::DEFAULT_ORIGIN,                // 'https://api.attestwire.com'; override for a self-hosted/staging deployment
);

$client->validate(
    data: $bytes,
    contentType: null,                             // force it instead of sniffing $data
    timeoutSeconds: 30.0,
);

use Attestwire\InvoiceSuite\AttestwireDocumentValidator;

$validator = AttestwireDocumentValidator::createFromFile($invoiceXmlFilename)
    ->setApiKey($attestwireApiKey)
    ->validate();

$validationWasSuccessful = !$validator->hasErrorMessagesInMessageBag()
    && !$validator->hasInternalErrorMessagesInMessageBag();

foreach ($validator->getMessageBag() as $messageBagItem) {
    echo "{$messageBagItem->getMessageSeverityValue()}: {$messageBagItem->getMessageContent()}\n";
}
bash
composer