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

Background information

Only synchronous moderation is supported for text anti-spam. For more information about parameters, see Synchronous text moderation.

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 Overview.

Prerequisites

The dependencies for Content Moderation SDK for PHP are installed. For more information about how to install the dependencies, 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 you add, block is returned for the suggestion parameter.

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

Operation Description Supported region
TextScanRequest Submits text moderation tasks. Set the scenes parameter to antispam.
  • cn-shanghai: China (Shanghai)
  • cn-beijing: China (Beijing)
  • cn-shenzhen: China (Shenzhen)
  • ap-southeast-1: Singapore (Singapore)
Sample code
<?php

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

try {
    // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
    AlibabaCloud::accessKeyClient('Your AccessKey ID', 'Your AccessKey secret')
        ->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()
        ->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;
}