API Usage Examples

Learn how to use the plugin's global functions to check FFL license status with FFL-DB.com

Available Global Functions

ffldb_is_on_file($ffl_number) Simple true/false check
ffldb_check_ffl($ffl_number) Full result with all details
ffldb_get_url($ffl_number) Get the FFL-DB.com URL
ffldb_is_connected() Check if API is configured
ffldb_get_instance() Get plugin instance for advanced usage
Example 1

Simple Boolean Check

The easiest way to check if an FFL is on file. Returns true if on file, false if not.
$ffl_number = '1-54-001-01-5K-04379';

if (ffldb_is_on_file($ffl_number)) {
    echo 'This FFL is verified and on file!';
} else {
    echo 'This FFL is NOT on file.';
}
Example 2

Full Status Check with Details

Get complete information about the FFL status including URL, expiration date, business name, and error info.
$ffl_number = '1-54-001-01-5K-04379';
$result = ffldb_check_ffl($ffl_number);

if ($result['on_file']) {
    echo 'FFL is ON FILE';
    echo 'View on FFL-DB: ' . $result['url'];
    
    if (!empty($result['expiration'])) {
        echo 'Expires: ' . $result['expiration'];
    }
    
    if (!empty($result['business_name'])) {
        echo 'Business: ' . $result['business_name'];
    }
} else {
    echo 'FFL is NOT on file';
    
    if (!empty($result['expired'])) {
        echo 'The license has expired.';
    }
}

Result Array Structure

'on_file'       => bool        // True if FFL is on file
'url'           => string|null // URL to view on FFL-DB.com
'expiration'    => string|null // License expiration date
'last_updated'  => string|null // When record was last updated
'business_name' => string|null // Business name if available
'error'         => string|null // Error message if check failed
'expired'       => bool        // True if license has expired
'cached_at'     => string      // When this result was cached
Example 3

Get FFL-DB URL

Get a direct link to view the FFL on FFL-DB.com
$ffl_number = '1-54-001-01-5K-04379';
$url = ffldb_get_url($ffl_number);

echo '<a href="' . esc_url($url) . '" target="_blank">View on FFL-DB</a>';

// Output: https://ffl-db.com/ffl/1-54-001-01-5K-04379
Example 4

Check if API is Connected

Verify the API is configured before making calls. Useful for conditional logic in your code.
if (ffldb_is_connected()) {
    // Safe to make API calls
    $result = ffldb_check_ffl('1-54-001-01-5K-04379');
} else {
    // API not configured - handle gracefully
    echo 'FFL verification is not available.';
}
Example 5

Using the Plugin Instance

For advanced usage, get the plugin instance directly. Provides access to additional methods like batch checking, cache control, and connection testing.
$ffldb = ffldb_get_instance();

// Check single FFL (same as ffldb_check_ffl)
$result = $ffldb->check_ffl_on_file('1-54-001-01-5K-04379');

// Bypass cache for fresh data
$result = $ffldb->check_ffl_on_file('1-54-001-01-5K-04379', true);

// Check multiple FFLs in batch
$ffl_numbers = array(
    '1-54-001-01-5K-04379',
    '1-54-002-02-6K-12345',
    '1-54-003-03-7K-67890'
);
$results = $ffldb->check_ffls_batch($ffl_numbers);

foreach ($results as $ffl_number => $status) {
    echo $ffl_number . ': ' . ($status['on_file'] ? 'ON FILE' : 'NOT ON FILE') . "\n";
}

// Clear cache for a specific FFL
$ffldb->clear_ffl_cache('1-54-001-01-5K-04379');

// Clear all cached data
$ffldb->clear_all_cache();

// Test API connection
$test = $ffldb->test_api_connection();
if ($test['success']) {
    echo 'API Connected!';
    echo 'Requests today: ' . $test['account_info']['requests_today'];
    echo 'Requests remaining: ' . $test['account_info']['requests_remaining'];
}
Example 6

Display a Status Badge

Create a simple function to display FFL verification status with colored badges.
function my_ffl_status_badge($ffl_number) {
    if (!ffldb_is_connected()) {
        return '';
    }
    
    $result = ffldb_check_ffl($ffl_number);
    
    if ($result['on_file']) {
        return '<span style="background:#28a745;color:#fff;padding:3px 8px;
                border-radius:3px;">✓ Verified</span>';
    } elseif (!empty($result['expired'])) {
        return '<span style="background:#dc3545;color:#fff;padding:3px 8px;
                border-radius:3px;">✗ Expired</span>';
    } else {
        return '<span style="background:#ffc107;color:#000;padding:3px 8px;
                border-radius:3px;">⚠ Not Verified</span>';
    }
}

// Usage:
echo my_ffl_status_badge('1-54-001-01-5K-04379');
Example 7

Check Expiration Status

Determine if an FFL license is expired or expiring soon using the Unix timestamp from the API.
function my_check_ffl_expiration($ffl_number) {
    $result = ffldb_check_ffl($ffl_number);
    
    // Check if already marked as expired by API
    if (!empty($result['expired'])) {
        return array('status' => 'expired', 'expiration' => $result['expiration']);
    }
    
    if (!$result['on_file']) {
        return array('status' => 'not_on_file');
    }
    
    // Use Unix timestamp if available (more reliable)
    if (!empty($result['expiration_timestamp'])) {
        $exp_time = (int) $result['expiration_timestamp'];
    } elseif (!empty($result['expiration'])) {
        $exp_time = strtotime($result['expiration']);
    } else {
        return array('status' => 'on_file', 'expiration' => 'unknown');
    }
    
    $days_remaining = floor(($exp_time - time()) / 86400);
    
    if ($days_remaining < 0) {
        return array(
            'status' => 'expired',
            'days' => abs($days_remaining),
            'expiration' => $result['expiration']
        );
    } elseif ($days_remaining <= 60) {
        return array(
            'status' => 'expiring_soon',
            'days' => $days_remaining,
            'expiration' => $result['expiration']
        );
    } else {
        return array(
            'status' => 'valid',
            'days' => $days_remaining,
            'expiration' => $result['expiration']
        );
    }
}

// Usage:
$expiration = my_check_ffl_expiration('1-54-001-01-5K-04379');

switch ($expiration['status']) {
    case 'not_on_file':
        echo 'FFL not on file';
        break;
    case 'expired':
        echo 'License expired ' . $expiration['days'] . ' days ago';
        break;
    case 'expiring_soon':
        echo 'Warning: License expires in ' . $expiration['days'] . ' days';
        break;
    case 'valid':
        echo 'License valid for ' . $expiration['days'] . ' more days';
        break;
}

Quick Reference

Global Functions

ffldb_is_on_file($ffl) bool
ffldb_check_ffl($ffl) array (on_file, url, expiration, expiration_timestamp, expired, etc.)
ffldb_check_ffl($ffl, true) array (bypass cache)
ffldb_get_url($ffl) string
ffldb_is_connected() bool
ffldb_get_instance() FFLDB_Connector object

Instance Methods

$ffldb->check_ffl_on_file($ffl) array
$ffldb->check_ffls_batch($array) array (keyed by FFL number)
$ffldb->get_ffl_url($ffl) string
$ffldb->is_api_verified() bool
$ffldb->test_api_connection() array (success, message, account_info)
$ffldb->clear_ffl_cache($ffl) bool
$ffldb->clear_all_cache() int (count cleared)