PHP code example of gender-api / client

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

    

gender-api / client example snippets




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
}

// 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"

// Localize by IP address
$result = $client->getByFirstNameAndClientIpAddress('Jan', '178.27.52.144');

// Localize by browser locale
$result = $client->getByFirstNameAndLocale('Jan', 'de_DE');

$result = $client->getByFirstNameAndLastName('Sandra Miller');

echo $result->getFirstName(); // "Sandra"
echo $result->getLastName();  // "Miller"
echo $result->getGender();    // "female"

$result = $client->getByFirstNameAndLastNameAndCountry(
    'Maria Garcia',
    'ES',
    strict: true
);

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

echo $result->getGender();       // "female"
echo $result->getFirstName();    // "Elisabeth" (extracted)
echo $result->getLastName();     // "Smith"

$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)

$results = $client->getByMultipleNamesAndCountry(['Andrea', 'Nicola'], 'IT');

$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();

$stats = $client->getStats();

echo $stats->getRemainingCredits(); // 4523
echo $stats->isLimitReached();       // false

$client = new GenderApiClient('your-api-key');
$client->setProxy('proxy.company.com', 3128);

$client = new GenderApiClient('your-api-key');
$client->setApiUrl('https://custom-api.example.com/');

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)
}