All Products
Search
Document Center

Content Moderation:Text anti-spam

Last Updated:Jul 31, 2023

This topic describes how to use Content Moderation SDK for PHP to moderate text for spam such as pornographic and terrorist content.

Description

Only synchronous moderation is supported for text anti-spam. For more information about the related parameters, see /green/text/scan.

You can send a request to moderate one or more text entries. You are charged based on the number of text entries that are moderated. For more information, see Billing overview.

Prerequisites

The dependencies for Content Moderation SDK for PHP are installed. For more information, see Installation.

Note

You must use the required PHP version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.

Moderate text for spam

Text anti-spam allows you to add custom terms, such as the brand terms of competitors. If the moderated text contains the terms that you have added, the value of the suggestion parameter is block in the returned machine-assisted moderation result.

You can add terms in the Content Moderation console or by calling an API operation.

Operation

Description

Supported region

TextScanRequest

Submits text moderation tasks with the scenes parameter set to antispam.

  • cn-shanghai: China (Shanghai)

  • cn-beijing: China (Beijing)

  • cn-shenzhen: China (Shenzhen)

  • ap-southeast-1: Singapore

Sample code

<?php

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

try {
   /**
     * Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
     * Common ways to obtain environment variables:
     * 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");
     */
    AlibabaCloud::accessKeyClient('We recommend that you obtain the AccessKey ID of your RAM user from environment variables', 'We recommend that you obtain the AccessKey secret of your RAM user from environment variables')
        ->timeout(10) // Set the timeout period to 10 seconds. This setting takes effect on the requests that are sent by using the client and that have no separate settings. 
        ->connectTimeout(3) // Set the connection timeout period to 3 seconds. If the value of this parameter is smaller than 1, the unit of this parameter is milliseconds. This setting takes effect on the requests that are sent by using the client and that have no separate settings. 
        ->regionId('cn-shanghai')
        ->asDefaultClient();

    $task1 = array('dataId' => 'ID of the text to be moderated',
        'content' => 'Content of the text to be moderated'
    );
    /**
     * Set the scenes parameter to antispam. 
     **/
    $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 of this parameter is smaller than 1, the unit of this parameter is milliseconds. This setting takes effect only on the current request. 
        ->body(json_encode(array('tasks' => array($task1), 'scenes' => array('antispam'), 'bizType' => 'Business scenario')))
        ->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;
}