Query a list of groups

Updated at:

Use the Content Moderation PHP software development kit (SDK) to retrieve all person group IDs in your account.

Prerequisites

Before you begin, ensure that you have:

Important

Use the PHP version specified in the Install PHP dependencies topic. Using an unsupported version causes operation calls to fail.

Query all person group IDs

The following example calls getGroups() to list all person group IDs in your account. The client connects to the AI Guardrails API endpoint in the cn-shanghai region. For available endpoints, see Endpoints.

For the full parameter reference, see Query a list of 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 the same client instance across requests to improve performance
    // and avoid repeated connection setup.
    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();

    // Call getGroups() to list all person group IDs.
    // Request-level timeouts override the client-level settings for this call only.
    $result = Green::v20180509()->getGroups()
        ->timeout(10)        // Request-level timeout: 10 seconds
        ->connectTimeout(3)  // Request-level connection timeout: 3 seconds
        ->request();

    print_r($result->toArray());

} catch (ClientException $exception) {
    // Client-side errors (network issues, invalid configuration)
    echo $exception->getMessage() . PHP_EOL;
} catch (ServerException $exception) {
    // Server-side errors (authentication failure, invalid parameters)
    echo $exception->getMessage() . PHP_EOL;
    echo $exception->getErrorCode() . PHP_EOL;
    echo $exception->getRequestId() . PHP_EOL;
    echo $exception->getErrorMessage() . PHP_EOL;
}

Before running this example, set the following environment variables:

Environment variableDescriptionExample
ALIBABA_CLOUD_ACCESS_KEY_IDAccessKey ID of your Resource Access Management (RAM) userLTAI5tXxx
ALIBABA_CLOUD_ACCESS_KEY_SECRETAccessKey secret of your RAM userxXxXxXx

What's next