Add a person to a group

Updated at:

Use the Content Moderation PHP SDK to add a person to one or more person groups.

Prerequisites

Before you begin, make sure that you have:

  • Installed the Content Moderation PHP SDK with the required PHP version. See Installation.

  • Configured the Content Moderation API endpoint. See Endpoints.

Important

Use the PHP version specified in the Installation guide. Using an unsupported version causes subsequent API calls to fail.

Usage notes

  • A person can belong to one or more person groups. Pass multiple group IDs in the groupIds array to add the person to several groups at once.

  • The personId and groupIds values must reference resources that already exist in your Content Moderation account.

  • For the full parameter reference, see API operation for adding a person to a group.

Add a person to a group

The following example calls addGroups() to add a person to one or more person groups.

<?php

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use AlibabaCloud\Green\Green;

try {
    // Initialize the client with credentials from environment variables.
    // Reuse this client instance across requests to improve 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 request timeout: 10 seconds
        ->connectTimeout(3)     // Client-level connection timeout: 3 seconds
        ->regionId('cn-shanghai')
        ->asDefaultClient();

    // Build the request body.
    // personId  - ID of the person to add. Required.
    // groupIds  - IDs of the target person groups. Required. Accepts multiple values.
    $person = [
        'personId' => '<person-id>',
        'groupIds' => ['<group-id>'],
    ];

    $result = Green::v20180509()->addGroups()
        ->timeout(10)           // Request-level timeout: 10 seconds (overrides client-level)
        ->connectTimeout(3)     // Request-level connection timeout: 3 seconds
        ->body(json_encode($person))
        ->request();

    print_r($result->toArray());

} catch (ClientException $exception) {
    echo $exception->getMessage() . PHP_EOL;
} catch (ServerException $exception) {
    echo $exception->getMessage() . PHP_EOL;
    echo $exception->getErrorCode() . PHP_EOL;
    echo $exception->getRequestId() . PHP_EOL;
    echo $exception->getErrorMessage() . PHP_EOL;
}

Replace the following placeholders before running the code:

PlaceholderDescription
<person-id>ID of the person to add to the group
<group-id>ID of the target person group. Add more entries to the groupIds array to assign the person to multiple groups

Next steps