Initialize the AI Guardrails SDK for PHP by creating a client and configuring your credentials and region before making API calls.
Prerequisites
Before you begin, ensure that you have:
An Alibaba Cloud account with an AccessKey ID and AccessKey secret. We recommend using a RAM user's AccessKey pair instead of your root account. To get one, see Obtain an AccessKey pair
A supported region for the content type you want to moderate. For the full list, see Endpoints
Create a client
The following example creates a client for moderating images, audio, videos, and text. It reads credentials from environment variables and sets the region to China (Shanghai).
// Load credentials from environment variables.
// Never hardcode your AccessKey ID or AccessKey secret in source code.
// Obtain the AccessKey ID of your RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
// Obtain the AccessKey secret of your RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
$accessKeyId = getenv('ALIBABA_CLOUD_ACCESS_KEY_ID');
$accessKeySecret = getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET');
AlibabaCloud::accessKeyClient($accessKeyId, $accessKeySecret)
->timeout(10) // Request timeout: 10 seconds
->connectTimeout(3) // Connection timeout: 3 seconds
->regionId('cn-shanghai')
->asDefaultClient();To use a different region, replace cn-shanghai with the target region ID. The following regions are supported:
| Region ID | Location |
|---|---|
cn-shanghai | China (Shanghai) |
cn-beijing | China (Beijing) |
cn-shenzhen | China (Shenzhen) |
ap-southeast-1 | Singapore |
Timeout parameters
| Parameter | Unit | Description |
|---|---|---|
timeout | Seconds | Maximum time to wait for a response. Applies to all requests that have no individual timeout set. |
connectTimeout | Seconds | Maximum time to wait for a connection to be established. If the value is less than 1, the unit is milliseconds. Applies to all requests that have no individual setting. |
Note: Store your AccessKey ID and AccessKey secret as environment variables (ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET) rather than hardcoding them in your code. This keeps credentials out of version control and makes it easier to rotate them.
Next steps
After the client is initialized, you can start submitting moderation requests for images, audio, video, and text content.