Get started with FacePing's face recognition API using PHP
First, create a new PHP project and an environment file:
mkdir faceping-php
cd faceping-php
touch .env
Add your API key to .env
:
FACEPING_API_KEY=your_api_key_here
Create a new file create-group.php
:
<?php
// Load environment variables
$env = parse_ini_file('.env');
$api_key = $env['FACEPING_API_KEY'];
function createGroup($groupName) {
global $api_key;
$options = [
'http' => [
'method' => 'POST',
'header' => [
'Authorization: Bearer ' . $api_key,
'Content-Type: application/json'
]
]
];
$context = stream_context_create($options);
try {
$response = file_get_contents(
"https://api.faceping.ai/groups/{$groupName}",
false,
$context
);
if ($response !== false) {
echo "Group created successfully\n";
return json_decode($response, true);
}
} catch (Exception $e) {
echo "Error creating group: " . $e->getMessage() . "\n";
}
return null;
}
// Usage
$result = createGroup('my-group');
print_r($result);
Create a new file upload-face.php
:
<?php
// Load environment variables
$env = parse_ini_file('.env');
$api_key = $env['FACEPING_API_KEY'];
function uploadFace($groupName, $imagePath) {
global $api_key;
if (!file_exists($imagePath)) {
echo "Error: Image file not found\n";
return null;
}
$imageData = file_get_contents($imagePath);
$boundary = uniqid();
$content = "--{$boundary}\r\n";
$content .= "Content-Disposition: form-data; name=\"image\"; filename=\"" . basename($imagePath) . "\"\r\n";
$content .= "Content-Type: " . mime_content_type($imagePath) . "\r\n\r\n";
$content .= $imageData . "\r\n";
$content .= "--{$boundary}--\r\n";
$options = [
'http' => [
'method' => 'POST',
'header' => [
'Authorization: Bearer ' . $api_key,
'Content-Type: multipart/form-data; boundary=' . $boundary,
'Content-Length: ' . strlen($content)
],
'content' => $content
]
];
$context = stream_context_create($options);
try {
$response = file_get_contents(
"https://api.faceping.ai/groups/{$groupName}/faces",
false,
$context
);
if ($response !== false) {
echo "Face uploaded successfully\n";
return json_decode($response, true);
}
} catch (Exception $e) {
echo "Error uploading face: " . $e->getMessage() . "\n";
}
return null;
}
// Usage
$result = uploadFace('my-group', 'path/to/face.jpg');
print_r($result);
Create a new file search-faces.php
:
<?php
// Load environment variables
$env = parse_ini_file('.env');
$api_key = $env['FACEPING_API_KEY'];
function searchFaces($groupName, $imagePath) {
global $api_key;
if (!file_exists($imagePath)) {
echo "Error: Image file not found\n";
return [];
}
$imageData = file_get_contents($imagePath);
$boundary = uniqid();
$content = "--{$boundary}\r\n";
$content .= "Content-Disposition: form-data; name=\"image\"; filename=\"" . basename($imagePath) . "\"\r\n";
$content .= "Content-Type: " . mime_content_type($imagePath) . "\r\n\r\n";
$content .= $imageData . "\r\n";
$content .= "--{$boundary}--\r\n";
$options = [
'http' => [
'method' => 'POST',
'header' => [
'Authorization: Bearer ' . $api_key,
'Content-Type: multipart/form-data; boundary=' . $boundary,
'Content-Length: ' . strlen($content)
],
'content' => $content
]
];
$context = stream_context_create($options);
try {
$response = file_get_contents(
"https://api.faceping.ai/groups/{$groupName}/search",
false,
$context
);
if ($response !== false) {
$data = json_decode($response, true);
$matches = $data['matches'] ?? [];
echo "Found " . count($matches) . " matches\n";
return $matches;
}
} catch (Exception $e) {
echo "Error searching faces: " . $e->getMessage() . "\n";
}
return [];
}
// Usage
$matches = searchFaces('my-group', 'path/to/search-face.jpg');
foreach ($matches as $match) {
echo "Match Score: {$match['score']}\n";
}