deflect/deflect-php
Composer 安装命令:
composer require deflect/deflect-php
包简介
PHP SDK for the Deflect Protection API
README 文档
README
PHP SDK for the Deflect Bot Protection API.
Requirements
- PHP 7.4 or higher
- cURL extension
- JSON extension
Installation
Install via Composer:
composer require deflect/deflect-php
Or add to your composer.json:
{
"require": {
"deflect/deflect-php": "^1.0"
}
}
Quick Start
use Deflect\Client; use Deflect\DeflectError; // Initialize the client $client = new Client([ 'api_key' => 'YOUR_API_KEY', 'action_id' => 'YOUR_ACTION_ID', ]); try { // Get verdict for a session token $verdict = $client->getVerdict(['token' => 'session-token-from-client']); 'token' => 'session-token-from-client', ]); // Check if user can pass if ($verdict->verdict !== null && $verdict->verdict->canPass === true) { echo "User allowed\n"; // Allow user to proceed } else { echo "User blocked\n"; // Block or challenge user } // Access additional information if ($verdict->ip !== null) { echo "IP Address: " . $verdict->ip->address . "\n"; echo "Is VPN: " . ($verdict->ip->isVPN ? 'Yes' : 'No') . "\n"; } if ($verdict->location !== null) { echo "Country: " . $verdict->location->country . "\n"; } } catch (DeflectError $e) { echo "Error: " . $e->getMessage() . "\n"; echo "Status: " . $e->getStatus() . "\n"; }
Intelligence APIs
The SDK also provides intelligence endpoints for analyzing emails, phone numbers, and IP addresses:
Email Intelligence
$emailIntel = $client->getEmailIntelligence('user@example.com'); if ($emailIntel->data !== null) { if (!$emailIntel->data->isValid) { echo "Invalid email format\n"; } if (!$emailIntel->data->isTrusted) { echo "Untrusted email domain\n"; } echo "Trust Score: " . $emailIntel->data->trustScore . "\n"; echo "Normalized: " . $emailIntel->data->normalizedEmail . "\n"; }
Phone Intelligence
$phoneIntel = $client->getPhoneIntelligence('+1234567890'); if ($phoneIntel->data !== null) { if (!$phoneIntel->data->isValid) { echo "Invalid phone number\n"; } echo "Country: " . $phoneIntel->data->country . "\n"; echo "Carrier: " . $phoneIntel->data->carrier . "\n"; echo "Line Type: " . $phoneIntel->data->lineType . "\n"; echo "Risk Score: " . $phoneIntel->data->riskScore . "\n"; if ($phoneIntel->data->isDisposable || $phoneIntel->data->isVirtual) { echo "Suspicious phone number\n"; } }
IP Intelligence
$ipIntel = $client->getIpIntelligence('8.8.8.8'); if ($ipIntel->data !== null) { if ($ipIntel->data->isVpn || $ipIntel->data->isProxy) { echo "VPN or Proxy detected\n"; } if ($ipIntel->data->isTorNode) { echo "Tor exit node detected\n"; } echo "Location: {$ipIntel->data->city}, {$ipIntel->data->country}\n"; echo "ISP: {$ipIntel->data->isp}\n"; }
Configuration Options
The Client constructor accepts an array with the following options:
| Option | Type | Required | Default | Description |
|---|---|---|---|---|
api_key |
string |
Yes | - | Your Deflect API key |
action_id |
string |
Yes* | - | Your action ID (*required for verdict, not for intelligence) |
base_url |
string |
No | https://api.deflect.bot |
Override base API URL |
timeout |
int |
No | 4 |
Request timeout in seconds |
max_retries |
int |
No | 0 |
Additional retry attempts on transient failures |
http_client |
object |
No | null |
Custom HTTP client (advanced) |
Note: action_id is only required when using getVerdict(). Intelligence endpoints (getEmailIntelligence(), getPhoneIntelligence(), getIpIntelligence()) only require api_key.
Example with Custom Options
$client = new Client([ 'api_key' => 'YOUR_API_KEY', 'action_id' => 'YOUR_ACTION_ID', 'base_url' => 'https://custom-api.deflect.bot', 'timeout' => 10, 'max_retries' => 2, ]);
Error Handling
All errors throw a DeflectError exception with the following methods:
try { $verdict = $client->getVerdict($token); } catch (DeflectError $e) { $message = $e->getMessage(); // Error message $status = $e->getStatus(); // HTTP status code (0 if not applicable) $body = $e->getBody(); // Response body (null if not applicable) }
Response Structures
VerdictResponse
The VerdictResponse object contains:
Properties
success(bool): Indicates if the request was successfulverdict(Verdict|null): Verdict decisioncanPass(bool|null): Whether the user should be allowed
device(Device|null): Device informationfingerprint(string|null): Device fingerprintuserAgent(string|null): User agent stringlanguages(string|null): Browser languagestimezone(string|null): Device timezoneos(string|null): Operating systemisMobile(bool|null): Is mobile device
ip(IPInfo|null): IP informationaddress(string|null): IP addresstype(string|null): IP type (e.g., "IPv4", "IPv6")isDatacenter(bool|null): Is datacenter IPisProxy(bool|null): Is proxyisTor(bool|null): Is Tor exit nodeisVPN(bool|null): Is VPNisThreat(bool|null): Is known threatisBogon(bool|null): Is bogon IPasn(string|null): ASN nameasnNumber(int|null): ASN number
location(Location|null): Geographic locationcity(string|null): City namepostalCode(string|null): Postal/ZIP codecountry(string|null): Country codecontinent(string|null): Continent codelatitude(float|null): Latitudelongitude(float|null): Longitude
session(Session|null): Session timingstartedAt(string|null): Session start time (ISO 8601)finishedAt(string|null): Session finish time (ISO 8601)
EmailIntelligenceResponse
success(bool): Request success statusdata(EmailIntelligenceData|null): Email analysis dataisValid(bool|null): Email format is validisTrusted(bool|null): Email domain is trustedtrustScore(int|null): Trust score (0-100)hasAliasing(bool|null): Email uses aliasing (+ or .)normalizedEmail(string|null): Normalized email addressdomain(string|null): Email domain
message(string|null): Additional message
PhoneIntelligenceResponse
success(bool): Request success statusdata(PhoneIntelligenceData|null): Phone analysis dataisValid(bool|null): Phone number is valide164Format(string|null): E.164 formatted numbercountryCode(string|null): Country codecountry(string|null): Country ISO codecountryName(string|null): Country namenumberType(string|null): Number typelineType(string|null): Line type (mobile, landline, etc.)carrier(string|null): Carrier namecarrierType(string|null): Carrier typeriskScore(int|null): Risk scoreisDisposable(bool|null): Is disposable numberisVirtual(bool|null): Is virtual numberisThreat(bool|null): Is known threatisSpam(bool|null): Is spam numberlastUpdated(string|null): Last update time
message(string|null): Additional message
IpIntelligenceResponse
success(bool): Request success statusdata(IpIntelligenceData|null): IP analysis dataisVpn(bool|null): Is VPNisDatacenterProxy(bool|null): Is datacenter proxyisResidentialProxy(bool|null): Is residential proxyisBogon(bool|null): Is bogon IPisTorNode(bool|null): Is Tor exit nodeisThreat(bool|null): Is known threatcity(string|null): City namepostalCode(string|null): Postal codecountry(string|null): Country codecontinent(string|null): Continent codelatitude(float|null): Latitudelongitude(float|null): LongitudeasnNumber(int|null): ASN numberasnOrganization(string|null): ASN organizationisp(string|null): ISP name
message(string|null): Additional message
Framework Integration
Laravel
// In your controller public function login(Request $request, Client $deflect) { $verdict = $deflect->getVerdict([ 'token' => $request->input('deflect_token'), 'email' => $request->input('email'), // Optional ]); if ($verdict->verdict?->canPass !== true) { return response()->json(['error' => 'Blocked'], 403); } }
License
MIT
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-28