定制 gender-api/client 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

gender-api/client

Composer 安装命令:

composer require gender-api/client

包简介

An easy to use API client for Gender-API.com

README 文档

README

PHP Version License: MIT PHPStan Level

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

📄 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.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. 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 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-04-10