PHP code example of ecourty / presidio-client

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

    

ecourty / presidio-client example snippets


use Ecourty\PresidioClient\Client\PresidioAnalyzer;
use Ecourty\PresidioClient\Client\PresidioAnonymizer;
use Ecourty\PresidioClient\Presidio;

// Use with default configuration (localhost:5001 for Analyzer, localhost:5002 for Anonymizer)
$presidio = new Presidio();

// Use with custom base URLs
$presidio = new Presidio(
    analyzer: new PresidioAnalyzer('http://presidio-analyzer:5001'),
    anonymizer: new PresidioAnonymizer('http://presidio-anonymizer:5002'),
);

// Analyze + Anonymize in a single call
$result = $presidio->anonymize('My email is [email protected] and my name is John Smith');

echo $result->getText();
// My email is <EMAIL_ADDRESS> and my name is <PERSON>

use Ecourty\PresidioClient\Enum\OperatorType;
use Ecourty\PresidioClient\Model\OperatorConfig;

$result = $presidio->anonymize(
    text: 'My name is John Smith, email: [email protected]',
    operators: [
        'PERSON' => new OperatorConfig(OperatorType::REPLACE, ['new_value' => '[REDACTED]']),
        'EMAIL_ADDRESS' => new OperatorConfig(OperatorType::MASK, [
            'masking_char' => '*',
            'chars_to_mask' => 12,
            'from_end' => false,
        ]),
    ],
);

use Ecourty\PresidioClient\Enum\OperatorType;
use Ecourty\PresidioClient\Model\DeanonymizeRequest;
use Ecourty\PresidioClient\Model\OperatorConfig;

$key = 'aaaaaaaaaaaaaaaa'; // 128-bit AES key

// Encrypt PII
$anonymized = $presidio->anonymize(
    text: 'My name is John Smith',
    operators: [
        'DEFAULT' => new OperatorConfig(OperatorType::ENCRYPT, ['key' => $key]),
    ],
);

// Decrypt PII
$deanonymized = $presidio->deanonymize(new DeanonymizeRequest(
    text: $anonymized->getText(),
    anonymizerResults: $anonymized->getItems(),
    deanonymizers: [
        'DEFAULT' => new OperatorConfig(OperatorType::DECRYPT, ['key' => $key]),
    ],
));

echo $deanonymized->getText();
// My name is John Smith

use Ecourty\PresidioClient\Enum\EntityType;

$result = $presidio->anonymize(
    text: 'Contact [email protected] or call 555-123-4567',
    entities: [EntityType::EMAIL_ADDRESS], // only detect emails
    scoreThreshold: 0.8,
);

use Ecourty\PresidioClient\Enum\AllowListMatch;

// "Acme Corp" will not be detected as an organization, but "John" will still be detected as a person.
$result = $presidio->anonymize(
    text: 'Contact John at Acme Corp',
    allowList: ['Acme Corp'],
    allowListMatch: AllowListMatch::EXACT,
);

use Ecourty\PresidioClient\Model\AdHocRecognizer;
use Ecourty\PresidioClient\Model\Pattern;

// Pattern-based recognizer
$zipRecognizer = new AdHocRecognizer(
    name: 'Zip code Recognizer',
    supportedLanguage: 'en',
    supportedEntity: 'ZIP',
    patterns: [new Pattern('zip code (weak)', '(\b\d{5}(?:\-\d{4})?\b)', 0.01)],
    context: ['zip', 'code'],
);

// Deny-list based recognizer
$titleRecognizer = new AdHocRecognizer(
    name: 'Title Recognizer',
    supportedLanguage: 'en',
    supportedEntity: 'TITLE',
    denyList: ['Mr', 'Mr.', 'Mrs', 'Ms'],
);

$result = $presidio->anonymize(
    text: 'Mr. Smith lives at zip code 12345',
    adHocRecognizers: [$zipRecognizer, $titleRecognizer],
);

use Ecourty\PresidioClient\Model\AnalyzerRequest;

$results = $presidio->analyze(new AnalyzerRequest(
    text: 'Call me at 555-123-4567',
    returnDecisionProcess: true,
));

foreach ($results as $result) {
    $explanation = $result->getAnalysisExplanation();
    if ($explanation !== null) {
        echo $explanation->getRecognizer();              // "PhoneRecognizer"
        echo $explanation->getPatternName();             // "US Phone Regex"
        echo $explanation->getOriginalScore();           // 0.8
        echo $explanation->getScoreContextImprovement(); // 0.15
    }

    $metadata = $result->getRecognitionMetadata();       // ['recognizer_name' => '...']
}

$health = $presidio->health();

$health->isHealthy();           // true if both services are up
$health->isAnalyzerHealthy();   // true if Analyzer is reachable
$health->isAnonymizerHealthy(); // true if Anonymizer is reachable

$presidio->getSupportedEntities(); // ['EMAIL_ADDRESS', 'PHONE_NUMBER', 'PERSON', ...]
$presidio->getRecognizers();       // [RecognizerResult, ...] (name, supported entities, languages)

$presidio->getAnonymizers();       // ['replace', 'redact', 'hash', 'mask', 'encrypt']
$presidio->getDeanonymizers();     // ['decrypt']

use Ecourty\PresidioClient\Exception\ApiException;

try {
    $analyzer->analyze($request);
} catch (ApiException $e) {
    echo $e->getStatusCode();   // 400
    echo $e->getResponseBody(); // {"error": "..."}
}

use Ecourty\PresidioClient\Client\PresidioAnalyzer;
use Ecourty\PresidioClient\Client\PresidioAnonymizer;
use Ecourty\PresidioClient\Presidio;
use Symfony\Component\HttpClient\HttpClient;

// Simple: custom base URLs
$presidio = new Presidio(
    analyzer: new PresidioAnalyzer('http://presidio-analyzer:5001'),
    anonymizer: new PresidioAnonymizer('http://presidio-anonymizer:5002'),
);

// Advanced: full HTTP client control
$analyzerClient = HttpClient::create(['base_uri' => 'http://presidio-analyzer:5001', 'timeout' => 10]);
$anonymizerClient = HttpClient::create(['base_uri' => 'http://presidio-anonymizer:5002', 'timeout' => 10]);

$presidio = new Presidio(
    analyzer: new PresidioAnalyzer(httpClient: $analyzerClient),
    anonymizer: new PresidioAnonymizer(httpClient: $anonymizerClient),
);

// Direct client access for advanced use cases
$analyzer = $presidio->getAnalyzer();
$anonymizer = $presidio->getAnonymizer();
bash
docker compose up -d
php examples/01-quickstart.php