PHP code example of contactreborn / php-client

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

    

contactreborn / php-client example snippets



ContactReborn\ContactRebornClient;
use ContactReborn\Enums\CheckResult;

// APIトークンでクライアントを初期化
$client = new ContactRebornClient('your-api-token-here');

// メールアドレスのチェック
$result = $client->checkEmail('[email protected]');

// Enumを使用した結果判定(推奨)
if (CheckResult::isBlocked($result['result'])) {
    echo "このメールアドレスはブロックされています: " . $result['reason'];
} elseif (CheckResult::isSafe($result['result'])) {
    echo "このメールアドレスは安全です";
} elseif (CheckResult::needsReview($result['result'])) {
    echo "このメールアドレスは確認が必要です";
}

$result = $client->checkEmail('[email protected]');

// レスポンス例:
// [
//     'result' => 'pass',  // CheckResult::PASS, BLOCK, SUSPICIOUS, UNKNOWN
//     'is_blocked' => false,
//     'reason' => null,
//     'matched_rule' => null,
//     'confidence' => 0.95,
//     'checked_at' => '2025-09-08 12:00:00'
// ]

use ContactReborn\Enums\CheckResult;

// 結果の種類
CheckResult::PASS       // 安全
CheckResult::BLOCK      // ブロック
CheckResult::SUSPICIOUS // 疑わしい
CheckResult::UNKNOWN    // 不明

// ヘルパーメソッド
CheckResult::isBlocked($result)    // ブロック判定
CheckResult::isSafe($result)       // 安全判定
CheckResult::needsReview($result)  // 要確認判定
CheckResult::getLabel($result)     // ラベル取得
CheckResult::getDescription($result) // 説明取得

use ContactReborn\Exceptions\ApiException;
use ContactReborn\Exceptions\AuthenticationException;
use ContactReborn\Exceptions\RateLimitException;

try {
    $result = $client->checkEmail('[email protected]');

} catch (AuthenticationException $e) {
    // 認証エラー(APIトークンが無効)
    echo "認証エラー: " . $e->getMessage();

} catch (RateLimitException $e) {
    // レート制限エラー
    echo "レート制限: " . $e->getMessage();
    $retryAfter = $e->getRetryAfter(); // 再試行までの秒数

} catch (ApiException $e) {
    // その他のAPIエラー
    echo "APIエラー: " . $e->getMessage();
}

// Contact Form 7 との統合
add_filter('wpcf7_validate', 'check_email_with_contactreborn', 10, 2);

function check_email_with_contactreborn($result, $tags) {
    $submission = WPCF7_Submission::get_instance();
    if (!$submission) return $result;

    $data = $submission->get_posted_data();
    $email = $data['your-email'] ?? '';

    if ($email) {
        $client = new ContactReborn\ContactRebornClient(CONTACTREBORN_API_TOKEN);

        try {
            $check = $client->checkEmail($email);

            if (ContactReborn\Enums\CheckResult::isBlocked($check['result'])) {
                $result->invalidate($tags[0], 'このメールアドレスは使用できません。');
            }
        } catch (Exception $e) {
            // エラー時は通過させる(サービス停止を防ぐ)
            error_log('Contact/Reborn API Error: ' . $e->getMessage());
        }
    }

    return $result;
}

$client = new ContactRebornClient($apiToken, [
    'timeout' => 60,  // 60秒のタイムアウト
]);

$client = new ContactRebornClient($apiToken, [
    'base_url' => 'https://staging-api.contact-reborn.net',
]);
bash
composer