ecourty/presidio-client 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ecourty/presidio-client

Composer 安装命令:

composer require ecourty/presidio-client

包简介

A PHP client for Microsoft Presidio — detect and anonymize PII in text via Presidio's REST APIs.

README 文档

README

PHP CI

A typed PHP client for Microsoft Presidio — detect and anonymize PII (Personally Identifiable Information) in text.

Requirements

Installation

composer require ecourty/presidio-client

Quick Start

The Presidio facade is the simplest way to use the library — it combines both clients and provides convenience methods:

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 john@example.com and my name is John Smith');

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

Custom Operators

You can configure different anonymization strategies per entity type:

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

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

Available operators: replace, redact, hash, mask, encrypt, decrypt, custom, keep.
Check the OperatorType enum for details.

Encrypt & Decrypt (Round-Trip)

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

Filter by Entity Type

use Ecourty\PresidioClient\Enum\EntityType;

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

Allow List

Exclude specific values from being detected as PII:

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,
);

Ad-Hoc Recognizers

Add custom recognizers on the fly without modifying the Presidio server:

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],
);

Decision Process (Explainability)

Get detailed explanations of why each entity was detected (requires low-level client access):

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 Checks

$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

List Capabilities

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

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

Supported Entity Types

The EntityType enum provides all supported PII types:

PERSON, PHONE_NUMBER, EMAIL_ADDRESS, CREDIT_CARD, CRYPTO, DATE_TIME, DOMAIN_NAME, IBAN_CODE, IP_ADDRESS, LOCATION, MEDICAL_LICENSE, NRP, SG_NRIC_FIN, UK_NHS, URL, US_BANK_NUMBER, US_DRIVER_LICENSE, US_ITIN, US_PASSPORT, US_SSN, AU_ABN, AU_ACN, AU_TFN, AU_MEDICARE, IN_PAN, IN_AADHAAR, IN_VEHICLE_REGISTRATION, IN_VOTER, IN_PASSPORT

Error Handling

API errors throw ApiException with the HTTP status code and response body:

use Ecourty\PresidioClient\Exception\ApiException;

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

All exceptions extend PresidioException (which extends RuntimeException).

Custom HTTP Client

The facade accepts pre-configured client instances. Each client accepts a Symfony HttpClientInterface:

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();

Examples

See the examples/ directory for runnable scripts:

docker compose up -d
php examples/01-quickstart.php

Development

# Start Presidio services
docker compose up -d

# Run unit tests
composer test-unit

# Run integration tests (requires Presidio services)
composer test-integration

# Run all tests
composer test

# Static analysis (PHPStan level max)
composer phpstan

# Code style check / fix
composer cs-check
composer cs-fix

# Full QA (PHPStan + CS check + tests)
composer qa

License

MIT

ecourty/presidio-client 适用场景与选型建议

ecourty/presidio-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 03 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「text」 「nlp」 「privacy」 「anonymization」 「gdpr」 「pii」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ecourty/presidio-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ecourty/presidio-client 我们能提供哪些服务?
定制开发 / 二次开发

基于 ecourty/presidio-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 37
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-19