Add a person

Updated at:

Use the Content Moderation PHP SDK to add a custom person to one or more person groups. A person holds identity information for face-based recognition, and groups organize persons for recognition tasks. A person must belong to at least one group when created.

Prerequisites

Before you begin, ensure that you have:

  • Installed the Content Moderation SDK for PHP. The SDK requires a specific PHP version; using an unsupported version causes operation calls to fail. See Installation

  • Created the person groups that the person will join. The groupIds parameter requires at least one group ID. See API operation for adding a person for group management APIs

  • Obtained the Content Moderation API endpoint for your region. See Endpoints

Add a person

<?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 client across requests to improve
    // moderation performance and avoid repeated connection overhead.
    AlibabaCloud::accessKeyClient(
        getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'),      // AccessKey ID of your RAM user
        getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET')   // AccessKey secret of your RAM user
    )
        ->timeout(10)         // Client-level timeout: 10 seconds
        ->connectTimeout(3)   // Client-level connection timeout: 3 seconds (if value < 1, unit is milliseconds)
        ->regionId('cn-shanghai')
        ->asDefaultClient();

    // Build the request body.
    // personId and groupIds are required; name and note are optional.
    $person = [
        'personId' => '<person-id>',
        'groupIds' => ['<group-id-0>', '<group-id-1>'],
        'name'     => '<person-name>',
        'note'     => '<remarks>',
    ];

    $result = Green::v20180509()->addPerson()
        ->timeout(10)         // Request-level timeout: 10 seconds
        ->connectTimeout(3)   // Request-level connection timeout: 3 seconds (if value < 1, unit is milliseconds)
        ->body(json_encode($person))
        ->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 with actual values:

PlaceholderDescription
<person-id>ID of the custom person.
<group-id-0>, <group-id-1>IDs of the groups the person will join.
<person-name>(Optional) Display name of the custom person.
<remarks>(Optional) Any additional notes about the person.

Parameters

ParameterTypeRequiredDescription
personIdStringYesThe ID of the custom person.
groupIdsArrayYesThe IDs of the groups to add the person to.
nameStringNoThe display name of the custom person.
noteStringNoRemarks or additional information about the person.

What's next