PHP icon

PHP Quick Start Guide

Get started with FacePing's face recognition API using PHP

Prerequisites

Setup

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

Step 1: Create Face Group

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

Step 2: Upload Face Image

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

Step 3: Search for Faces

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";
}

Security Notes

  • Images are immediately discarded after vector conversion
  • Face vectors cannot be reverse-engineered into images
  • Always use HTTPS for all API calls
  • Store your API keys in environment variables (.env file)

Ready to get started?

Take a look at the API documentation
API docs