Use the Content Moderation PHP SDK to search a person group for faces that match a given image. The API returns the top 5 most similar persons, ranked by similarity score in descending order.
This SDK accepts image URLs only. Local files and binary data are not supported. URLs must be HTTP or HTTPS and no longer than 2,048 characters.
How it works
Submit an image URL along with a
groupIdto specify which person group to search.The
imageSyncScanAPI scans the image against the person group using thesface-nscene.The API returns the top 5 matching persons from the group, sorted by similarity score.
Prerequisites
Before you begin, ensure that you have:
The PHP SDK installed using the PHP version specified in the Install PHP dependencies topic. Using an unsupported version causes subsequent API calls to fail.
Your AccessKey ID and AccessKey secret from your RAM user, stored as environment variables.
The AI Guardrails endpoint for your region. See Endpoints.
A person group created with faces indexed before running a retrieval task. See API operation for custom face retrieval.
Submit a face retrieval task
The request requires two parameters: the URL of the image to match (url) and the ID of the person group to search (groupId). Pass groupId inside the extras object of the task.
| Parameter | Required | Description |
|---|---|---|
url | Yes | HTTP or HTTPS URL of the face image. Maximum 2,048 characters. |
groupId | Yes | ID of the person group to search. Specify exactly one group per request. |
dataId | No | Custom identifier for the task, used for tracking. |
<?php
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use AlibabaCloud\Green\Green;
try {
// Initialize the client. Reuse this instance across requests to improve
// performance and avoid repeated connection overhead.
AlibabaCloud::accessKeyClient(
getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'),
getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET')
)
->timeout(10) // Request timeout: 10 seconds
->connectTimeout(3) // Connection timeout: 3 seconds
->regionId('cn-shanghai')
->asDefaultClient();
// Build the retrieval task.
$task = [
'dataId' => '<your-data-id>',
'url' => '<image-url>',
'extras' => ['groupId' => '<group-id>'],
];
// Submit the task using the sface-n scene (custom face retrieval).
$result = Green::v20180509()->imageSyncScan()
->timeout(10)
->connectTimeout(3)
->body(json_encode([
'tasks' => [$task],
'scenes' => ['sface-n'],
]))
->request();
print_r($result->toArray());
} catch (ClientException $e) {
echo $e->getMessage() . PHP_EOL;
} catch (ServerException $e) {
echo $e->getMessage() . PHP_EOL;
echo $e->getErrorCode() . PHP_EOL;
echo $e->getRequestId() . PHP_EOL;
echo $e->getErrorMessage() . PHP_EOL;
}Replace the following placeholders before running the code:
| Placeholder | Description | Example |
|---|---|---|
<your-data-id> | Custom task identifier for tracking | task-001 |
<image-url> | HTTP or HTTPS URL of the face image | https://example.com/face.jpg |
<group-id> | ID of the person group to search | group-abc123 |
What's next
Review the full list of request and response parameters: API operation for custom face retrieval
Find the correct endpoint for your region: Endpoints