gender-api/client
Composer 安装命令:
composer require gender-api/client
包简介
An easy to use API client for Gender-API.com
README 文档
README
A modern, type-safe PHP client for the Gender-API.com service. Determine gender from first names, full names, or email addresses with high accuracy across 200+ countries.
✨ Features
- 🔍 Name Gender Detection - Detect gender from first names with 99%+ accuracy
- 🌍 Country Localization - Improve accuracy with country-specific results
- 📧 Email Analysis - Extract and analyze names from email addresses
- 🗺️ Country of Origin - Discover name origins and geographic distribution
- 📊 Batch Processing - Query multiple names in a single API call
- 🔒 Type-Safe - Full PHP 8.3+ support with strict typing
- ⚡ Modern - PSR-4 autoloading, PHPUnit 11, PHPStan Level 6
📦 Installation
Install via Composer:
composer require gender-api/client
🔑 API Key
Get your free API key at: gender-api.com/account
🚀 Quick Start
<?php use GenderApi\Client as GenderApiClient; $client = new GenderApiClient('your-api-key'); // Simple gender lookup $result = $client->getByFirstName('Elisabeth'); if ($result->genderFound()) { echo $result->getGender(); // "female" echo $result->getAccuracy(); // 99 }
📖 Usage Examples
First Name with Country
For names that vary by country (e.g., "Andrea" is male in Italy, female in Germany):
// In Italy, Andrea is typically male $result = $client->getByFirstNameAndCountry('Andrea', 'IT'); echo $result->getGender(); // "male" // In Germany, Andrea is typically female $result = $client->getByFirstNameAndCountry('Andrea', 'DE'); echo $result->getGender(); // "female"
First Name with Localization
Detect country from IP address or browser locale:
// Localize by IP address $result = $client->getByFirstNameAndClientIpAddress('Jan', '178.27.52.144'); // Localize by browser locale $result = $client->getByFirstNameAndLocale('Jan', 'de_DE');
Full Name (First + Last Name Split)
The API automatically splits full names and identifies the first name:
$result = $client->getByFirstNameAndLastName('Sandra Miller'); echo $result->getFirstName(); // "Sandra" echo $result->getLastName(); // "Miller" echo $result->getGender(); // "female"
With country and strict mode:
$result = $client->getByFirstNameAndLastNameAndCountry( 'Maria Garcia', 'ES', strict: true );
Email Address
Extract and analyze names from email addresses:
$result = $client->getByEmailAddress('elisabeth.smith@company.com'); echo $result->getGender(); // "female" echo $result->getFirstName(); // "Elisabeth" (extracted) echo $result->getLastName(); // "Smith"
Multiple Names (Batch)
Query multiple names in a single API call for efficiency:
$names = ['Michael', 'Sarah', 'Kim', 'Jordan']; $results = $client->getByMultipleNames($names); foreach ($results as $result) { printf( "%s: %s (%d%% confidence)\n", $result->getFirstName(), $result->getGender(), $result->getAccuracy() ); } // Output: // Michael: male (99% confidence) // Sarah: female (99% confidence) // Kim: female (72% confidence) // Jordan: male (68% confidence)
With country filter:
$results = $client->getByMultipleNamesAndCountry(['Andrea', 'Nicola'], 'IT');
Country of Origin
Discover where a name originates from:
$result = $client->getCountryOfOrigin('Giuseppe'); echo $result->getGender(); // "male" foreach ($result->getCountryOfOrigin() as $country) { printf( "%s (%s): %.0f%%\n", $country->getCountryName(), $country->getCountry(), $country->getProbability() * 100 ); } // Output: // Italy (IT): 89% // Argentina (AR): 4% // United States (US): 3% // Get interactive map URL echo $result->getCountryOfOriginMapUrl();
Account Statistics
Monitor your API usage:
$stats = $client->getStats(); echo $stats->getRemainingCredits(); // 4523 echo $stats->isLimitReached(); // false
⚙️ Configuration
Proxy Server
$client = new GenderApiClient('your-api-key'); $client->setProxy('proxy.company.com', 3128);
Custom API URL
For enterprise or on-premise installations:
$client = new GenderApiClient('your-api-key'); $client->setApiUrl('https://custom-api.example.com/');
🧪 Development
Requirements
- PHP 8.3 or higher
- Composer
Setup
# Clone the repository git clone https://github.com/markus-perl/gender-api-client.git cd gender-api-client # Install dependencies composer install # Start Docker environment (optional) docker-compose up -d
Running Tests
# Unit tests (with mocked data) ./vendor/bin/phpunit --testsuite unit # Integration tests (requires API key in .env) cp .env.example .env # Edit .env and add your API key ./vendor/bin/phpunit --testsuite integration # All tests ./vendor/bin/phpunit --testsuite unit,integration
Static Analysis
./vendor/bin/phpstan analyse
🔍 API Response Properties
SingleName Result
| Property | Type | Description |
|---|---|---|
getFirstName() |
?string |
The queried name |
getProbability() |
?float |
Probability (e.g. 0.99) |
getGender() |
?string |
"male", "female", or "unknown" |
getAccuracy() |
?int |
Confidence percentage (0-100) |
getSamples() |
?int |
Number of data samples |
getCountry() |
?string |
ISO 3166-1 country code |
genderFound() |
bool |
Whether a gender was determined |
Stats Result
| Property | Type | Description |
|---|---|---|
getRemainingCredits() |
?int |
API credits remaining |
isLimitReached() |
?bool |
Whether quota is exhausted |
🛠️ Error Handling
use GenderApi\Client as GenderApiClient; use GenderApi\Client\ApiException; use GenderApi\Client\RuntimeException; use GenderApi\Client\InvalidArgumentException; use GenderApi\Client\Downloader\NetworkErrorException; try { $result = $client->getByFirstName('Elisabeth'); } catch (InvalidArgumentException $e) { // Invalid input parameters } catch (ApiException $e) { // API returned an error (e.g., invalid key, limit exceeded) echo $e->getCode(); // Error code echo $e->getMessage(); // Error message } catch (NetworkErrorException $e) { // Network connectivity issues } catch (RuntimeException $e) { // Other runtime errors (e.g., missing API key) }
📚 Resources
- Homepage: gender-api.com
- API Documentation: gender-api.com/api-docs
- OpenAPI Specification: docs/openapi.yml
- FAQ: gender-api.com/faq
- Error Codes: gender-api.com/error-codes
- Support: gender-api.com/contact
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
gender-api/client 适用场景与选型建议
gender-api/client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 574.57k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2017 年 04 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「genderize」 「gender api」 「gender of names」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 gender-api/client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 gender-api/client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 gender-api/client 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Fluent translations for Laravel
Yii2 extension of Gender API
A Laravel Nova gender field.
A PSR-7 compatible library for making CRUD API endpoints
A simple PHP library for parsing complex Malay names into their individual components
Getter and Setter package for model gender. This package is part of a project 'Aedart\Model', visit https://bitbucket.org/aedart/model to learn more about it. Official sub-package website (https://bitbucket.org/fenix440/model-gender/).
统计信息
- 总下载量: 574.57k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 18
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-04-10