Text anti-spam

Updated at:

Use AI Guardrails SDK for PHP to scan user-generated text — such as chat messages, comments, and posts — for spam content including pornographic and terrorist material, and get a synchronous moderation result.

How it works

Only synchronous moderation is supported for text anti-spam.

Submit a text moderation request with the scenes parameter set to antispam. AI Guardrails evaluates each text entry and returns a suggestion value of block when the text violates policy.

Each request can include one or more text entries. You are charged per text entry moderated. For pricing details, see Billing overview.

Text anti-spam also supports custom terms — for example, competitor brand names. If a moderated text entry contains a custom term, the suggestion value is block. Add custom terms in the AI Guardrails console or by calling an API operation.

Prerequisites

Before you begin, make sure that you have:

Important

Using an unsupported PHP version causes operation calls to fail. Check the required version in the installation guide before installing.

Scan text for spam

The TextScanRequest operation submits text moderation tasks with scenes set to antispam. The following regions are supported:

Region IDLocation
cn-shanghaiChina (Shanghai)
cn-beijingChina (Beijing)
cn-shenzhenChina (Shenzhen)
ap-southeast-1Singapore

Set your credentials as environment variables before running the sample code:

export ALIBABA_CLOUD_ACCESS_KEY_ID=<your-access-key-id>
export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<your-access-key-secret>

Replace the following placeholders in the sample code before running it:

PlaceholderDescription
<your-data-id>A unique ID for the text entry you are moderating
<your-text-content>The text content to moderate
<your-biz-type>Your business scenario identifier

Reuse the client instance across requests to avoid repeated connection overhead.

<?php

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

try {
    AlibabaCloud::accessKeyClient(
        getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'),
        getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET')
    )
        ->timeout(10)           // Set the timeout period to 10 seconds. This setting takes effect on requests sent by using the client that have no separate settings.
        ->connectTimeout(3)     // Set the connection timeout period to 3 seconds. If the value is smaller than 1, the unit is milliseconds. This setting takes effect on requests sent by using the client that have no separate settings.
        ->regionId('cn-shanghai')
        ->asDefaultClient();

    $task1 = [
        'dataId'  => '<your-data-id>',
        'content' => '<your-text-content>',
    ];

    $result = Green::v20180509()->textScan()
        ->timeout(10)           // Set the timeout period to 10 seconds. This setting takes effect only on the current request.
        ->connectTimeout(3)     // Set the connection timeout period to 3 seconds. If the value is smaller than 1, the unit is milliseconds. This setting takes effect only on the current request.
        ->body(json_encode([
            'tasks'   => [$task1],
            'scenes'  => ['antispam'],
            'bizType' => '<your-biz-type>',
        ]))
        ->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;
}

For the full list of request and response parameters, see /green/text/scan.

What's next