PHP SDK Documentation
Requirements
- PHP 8.1 or higher
- Composer
- ext-curl or Guzzle
Installation
composer require unikhorn/php-sdk
Configuration
<?php
require_once 'vendor/autoload.php';
use Unikhorn\SDK\UnikhornClient;
use Unikhorn\SDK\Exception\UnikhornException;
// Initialize the client with your API key
$client = new UnikhornClient([
'api_key' => 'votre-cle-api',
'base_url' => 'https://api.unikhorn.com', // Optionnel
'timeout' => 30, // Optionnel, timeout en secondes
'language' => 'fr' // Optional, default language
]);
Usage Examples
Calculate a Coat
<?php
try {
// Simple calculation with base genes only
$result = $client->calculateCoat([
'base' => [
'agouti' => ['A','A'],
'extension' => ['E','E']
],
'lang' => 'fr' // Optionnel
]);
echo "Calculated coat: " . $result->getTranslate() . "\n";
echo "Complete genotype: \n";
print_r($result->getBase());
} catch (UnikhornException $e) {
echo "Error: " . $e->getMessage();
}
Calculation with Modifier Genes
<?php
try {
$result = $client->calculateCoat([
'base' => [
'agouti' => ['A','a'],
'extension' => ['E','e']
],
'others' => [
// Dilution genes
'matp' => ['Cr','n'], // MATP (cream/pearl)
'dun' => ['D','nd2'], // Dun gene
'silver' => ['Z','n'], // Silver gene
'champagne' => ['Ch','n'], // Champagne gene
'grey' => ['G','n'], // Grey gene
// Pattern/white genes
'kit' => ['DW2','n'], // KIT Dominant White 2
'lp' => ['Lp','n'], // Leopard Complex
'lwo' => ['n','n'], // Lethal White Overo
'mushroom' => ['Mu','n'], // Mushroom
'splash_mift' => ['n','n'], // Splash MIFT
'splash_pax3' => ['SW1','n'] // Splash PAX3
],
'lang' => 'fr'
]);
echo "Coat: " . $result->getTranslate() . "\n";
echo "Base genes: \n";
print_r($result->getBase());
echo "Modifier genes: \n";
print_r($result->getOthers());
} catch (UnikhornException $e) {
echo "Error: " . $e->getMessage();
}
Generate Offspring Possibilities
<?php
try {
$offspring = $client->generateOffspring([
'mother' => [
'base' => [
'agouti' => ['A','a'],
'extension' => ['E','e']
],
'others' => [
'matp' => ['Cr','n'],
'dun' => ['D','nd1']
]
],
'father' => [
'base' => [
'agouti' => ['A','A'],
'extension' => ['e','e']
],
'others' => [
'matp' => ['n','n'],
'dun' => ['D','D']
]
],
'lang' => 'fr'
]);
echo "Number of possibilities: " . count($offspring->getResults()) . "\n\n";
foreach ($offspring->getResults() as $possibility) {
echo sprintf(
"- %s (%.2f%%)\n Genotype: %s %s\n",
$possibility['coat'],
$possibility['percentage'],
$possibility['genotype']['agouti'],
$possibility['genotype']['extension']
);
}
// Display possibilities grouped by coat
$grouped = $offspring->getGroupedByCoat();
foreach ($grouped as $coat => $percentage) {
echo sprintf("%s : %.2f%%\n", $coat, $percentage);
}
} catch (UnikhornException $e) {
echo "Error: " . $e->getMessage();
}
KIT Genetic Linkage
<?php
// For horses with KIT-linked genes (tobiano, sabino, etc.)
try {
$offspring = $client->generateOffspring([
'mother' => [
'base' => [
'agouti' => ['A','a'],
'extension' => ['E','e']
],
'others' => [
'kit' => ['DW2','n']
]
],
'father' => [
'base' => [
'agouti' => ['A','A'],
'extension' => ['e','e']
],
'others' => [
'kit' => ['n','n']
]
],
'kit_linkage' => [
'mother' => ['E', 'DW2'], // Extension E linked with DW2
'father' => []
],
'lang' => 'fr'
]);
if ($offspring->hasKitLinkage()) {
echo "⚠️ KIT genetic linkage detected\n";
echo "Results account for genetic linkage.\n\n";
}
foreach ($offspring->getResults() as $result) {
echo sprintf("%s : %.2f%%\n", $result['coat'], $result['percentage']);
}
} catch (UnikhornException $e) {
echo "Error: " . $e->getMessage();
}
Available Methods
UnikhornClient
Method | Description | Return |
---|---|---|
calculateCoat(array $data) |
Calculate a horse's coat | CoatResult |
generateOffspring(array $data) |
Generate offspring possibilities | OffspringResult |
getApiKeyInfo() |
Retrieve API key information | ApiKeyInfo |
setLanguage(string $lang) |
Set default language | void |
CoatResult
Method | Description | Return |
---|---|---|
getTranslate() |
Translated coat name | string |
getBase() |
Base genes (agouti, extension) | array |
getOthers() |
Modifier genes | array |
OffspringResult
Method | Description | Return |
---|---|---|
getResults() |
Complete list of possibilities | array |
getGroupedByCoat() |
Results grouped by coat | array |
hasKitLinkage() |
Indicates if KIT linkage present | bool |
hasIDK() |
Indicates if unknown genes present | bool |
Error Handling
<?php
use Unikhorn\SDK\Exception\UnikhornException;
use Unikhorn\SDK\Exception\AuthenticationException;
use Unikhorn\SDK\Exception\ValidationException;
use Unikhorn\SDK\Exception\RateLimitException;
try {
$result = $client->calculateCoat($data);
} catch (AuthenticationException $e) {
// Authentication error (invalid API key)
echo "Authentication error: " . $e->getMessage();
} catch (ValidationException $e) {
// Data validation error
echo "Invalid data: " . $e->getMessage();
print_r($e->getErrors()); // Validation error details
} catch (RateLimitException $e) {
// Rate limit exceeded
echo "Too many requests. Retry in " . $e->getRetryAfter() . " seconds.";
} catch (UnikhornException $e) {
// Other API error
echo "API error: " . $e->getMessage();
} catch (\Exception $e) {
// General error
echo "Unexpected error: " . $e->getMessage();
}