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;

try {

    $apiClient = new GenderApiClient('insert your API key');
    $name = $apiClient->getByFirstName('elisabeth');

    if ($name->genderFound()) {
        echo $name->getGender(); // will return "female" (possible values: male, female, unknown)
    }

} catch (GenderApi\Exception $e) {
    // Name lookup failed due to a network error or insufficient requests left
    // See https://gender-api.com/en/api-docs/error-codes
    echo 'Exception: ' . $e->getMessage();
}

use GenderApi\Client as GenderApiClient;

try {

    $apiClient = new GenderApiClient('insert your API key');
    

use GenderApi\Client as GenderApiClient;

try {

    $apiClient = new GenderApiClient('insert your API key');
    
    // Get gender by email address name and country
    $name = $apiClient->getByEmailAddress('[email protected]');
    if ($name->genderFound()) {
        echo $name->getGender(); // will return "female"
    }
    
    // Get gender by email address name and country
    $name = $apiClient->getByEmailAddressAndCountry('[email protected]', 'US');
    echo $name->getGender(); // will return "female"
    if ($name->genderFound()) {
        echo $name->getGender(); // will return "female"
    }
    
} catch (GenderApi\Exception $e) {
    // Name lookup failed due to a network error or insufficient requests left
    // See https://gender-api.com/en/api-docs/error-codes
    echo 'Exception: ' . $e->getMessage();
}

use GenderApi\Client as GenderApiClient;

try {

    $apiClient = new GenderApiClient('insert your API key');
    
    // Get gender by email address name and country
    $name = $apiClient->getByFirstNameAndLastName('Frank Underwood');
 
    if ($name->genderFound()) {
        echo $name->getGender(); // will return "male"
        echo $name->getFirstName(); // will return "Frank"
        echo $name->getLastName(); // will return "Underwood"
    }

} catch (GenderApi\Exception $e) {
    // Name lookup failed due to a network error or insufficient requests left
    // See https://gender-api.com/en/api-docs/error-codes
    echo 'Exception: ' . $e->getMessage();
}

use GenderApi\Client as GenderApiClient;

try {

    $apiClient = new GenderApiClient('insert your API key');
    
    // Get gender by email address name and country
    $name = $apiClient->getCountryOfOrigin('Frank');
 
    if ($name->genderFound()) {
        echo $name->getGender(); // will return "male"
        echo $name->getFirstName(); // will return "Frank"
        echo $name->getLastName(); // will return "Underwood"
        
        echo $name->getCountryOfOriginMapUrl(); // will return a link to a map that displays the result in a rendered for
        
        foreach ($name->getCountryOfOrigin() as $country) {
                var_dump($country); // country of origin
        }
        
    }

} catch (GenderApi\Exception $e) {
    // Name lookup failed due to a network error or insufficient requests left
    // See https://gender-api.com/en/api-docs/error-codes
    echo 'Exception: ' . $e->getMessage();
}

use GenderApi\Client as GenderApiClient;

try {

    $apiClient = new GenderApiClient('insert your API key');
    
    $stats = $apiClient->getStats();
    
    // Check your query limit
    if ($stats->isLimitReached()) {
        echo "query limit reached.";
    }
    
    // Get remaining requests
    echo $stats->getRemainingRequests() . ' requests left.';

} catch (GenderApi\Exception $e) {
    // Name lookup failed due to a network error
    // See https://gender-api.com/en/api-docs/error-codes
    echo 'Exception: ' . $e->getMessage();
}

use GenderApi\Client as GenderApiClient;

$apiClient = new GenderApiClient('insert your API key');
$apiClient->setProxy('localhost', 3128);