Query a list of groups
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:
Installed the PHP dependencies for the required PHP version. For instructions, see Install PHP dependencies.
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 variable | Description | Example |
|---|---|---|
ALIBABA_CLOUD_ACCESS_KEY_ID | AccessKey ID of your Resource Access Management (RAM) user | LTAI5tXxx |
ALIBABA_CLOUD_ACCESS_KEY_SECRET | AccessKey secret of your RAM user | xXxXxXx |
What's next
To create a person group, see Create a group.
For all available Content Moderation PHP SDK operations, see PHP SDK overview.