This topic shows you how to use SDKs for PHP to call text anti-spam operations and moderate text for spam such as pornographic and terrorist content.

Background information

Only synchronous moderation is supported for the text anti-spam operation.

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

Before you begin

Before you call operations, make the following preparations:

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, the value of the suggestion parameter is block in the returned machine-assisted moderation results.

You can add terms in the Alibaba Cloud Content Moderation console or by calling the corresponding operation. The terms are UTF-8-encoded. For more information about the operation, see Text moderation.

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

include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
use Green\Request\V20180509 as Green;

$iClientProfile = DefaultProfile::getProfile("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret");
DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
$client = new DefaultAcsClient($iClientProfile);
$request = new Green\TextScanRequest();
$request->setMethod("POST");
$request->setAcceptFormat("JSON");
$task1 = array('dataId' =>  uniqid(),
    'content' => 'Text to be moderated'
);

/**
 * Set the scenes parameter to antispam.
 **/
$request->setContent(json_encode(array("tasks" => array($task1),
    "scenes" => array("antispam"))));
try {
    $response = $client->getAcsResponse($request);
    print_r($response);
    if(200 == $response->code){
        $taskResults = $response->data;
        foreach ($taskResults as $taskResult) {
            if(200 == $taskResult->code){
                $sceneResults = $taskResult->results;
                foreach ($sceneResults as $sceneResult) {
                    $scene = $sceneResult->scene;
                    $suggestion = $sceneResult->suggestion;
                    // Take a further action on the text based on the values of the scene and suggestion parameters.
                    // Take different actions on the text based on the different values of the suggestion parameter. For example, delete the illegal data.
                }
            }else{
                print_r("task process fail:" + $response->code);
            }
        }
    }else{
        print_r("detect not success. code:" + $response->code);
    }
} catch (Exception $e) {
    print_r($e);
}

Provide feedback on text anti-spam results

If a text moderation result does not meet your expectations, you can call the TextFeedbackRequest operation to provide feedback on the machine-assisted moderation result.

The server corrects the text anti-spam result based on your feedback and adds the feedback to a text pattern blacklist or whitelist. When you submit a text pattern next time, the server returns the text anti-spam result based on the label that you passed by calling the TextFeedbackRequest operation. For more information about the operation, see Give feedback on moderation results.

Operation Description Supported region
TextFeedbackRequest Uses the feedback provided by you to correct the text anti-spam result generated by the text anti-spam algorithm.
  • cn-shanghai
  • cn-beijing
  • cn-shenzhen
  • ap-southeast-1
  • us-west-1
Sample code
<? php

include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
use Green\Request\V20180509 as Green;

$iClientProfile = DefaultProfile::getProfile("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret");
DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
$client = new DefaultAcsClient($iClientProfile);
$request = new Green\TextFeedbackRequest();
$request->setMethod("POST");
$request->setAcceptFormat("JSON");
$task1 = array('dataId' =>  uniqid(),
    'content' => 'Text to be moderated'
);

/**
 * Set the scenes parameter to antispam.
 **/
$request->setContent(json_encode(array("taskId" => 'test taskId', "content" => "Text to be moderated",
    "label" => 'spam')));
try {
    $response = $client->getAcsResponse($request);
    print_r($response);
} catch (Exception $e) {
    print_r($e);
}