This topic describes how to use Content Moderation SDK for PHP to manage custom text libraries. This meets your personalized requirements for text anti-spam.

Background information

Based on the text type, text libraries are classified into term libraries and text pattern libraries. Based on the management purposes, text libraries are classified into whitelists, blacklists, and review lists. For more information about parameters, see DescribeKeywordLib.

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.

Query text libraries

  • Query term libraries
    <?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();
    
        $response = Green::v20170823()->describeKeywordLib()
            ->withServiceModule('open_api')
            ->request();
        print_r($response);
        $allLibs = $response->KeywordLibList;
        $textAntispamKeywordLibs = array();
        foreach ($allLibs as $keywordLib) {
            $libType = $keywordLib->LibType;
            $resourceType = $keywordLib->ResourceType;
            $source = $keywordLib->Source;
            # Query custom term libraries. 
            if ("textKeyword" == $libType and "TEXT" == $resourceType and "MANUAL" == $source) {
                array_push($textAntispamKeywordLibs, $keywordLib);
            }
            # Query feedback-based term libraries. 
            if ("textKeyword" == $libType and "TEXT" == $resourceType and "FEEDBACK" == $source) {
                array_push($textAntispamKeywordLibs, $keywordLib);
            }
        }
        print_r($textAntispamKeywordLibs);
    } 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;
    }
  • Query text pattern libraries, including custom text pattern libraries and feedback-based text pattern libraries
    <?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();
    
        $response = Green::v20170823()->describeKeywordLib()
            ->withServiceModule('open_api')
            ->request();
        print_r($response->toArray());
        $allLibs = $response->KeywordLibList;
        $similarTextLibs = array();
        foreach ($allLibs as $keywordLib) {
            $libType = $keywordLib->LibType;
            $resourceType = $keywordLib->ResourceType;
            $source = $keywordLib->Source;
            # Query the custom term libraries for text anti-spam. 
            if ('similarText' == $libType and 'TEXT' == $resourceType and 'MANUAL' == $source) {
                array_push($similarTextLibs, $keywordLib);
            }
            # Query the feedback-based term libraries for text anti-spam. 
            if ('similarText' == $libType and 'TEXT' == $resourceType and 'FEEDBACK' == $source) {
                array_push($similarTextLibs, $keywordLib);
            }
        }
        print_r($similarTextLibs);
    } 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;
    }

Create text libraries

  • Create a term library
    <?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();
    
        $response = Green::v20170823()->createKeywordLib()
            ->withServiceModule('open_api')
            ->withName ('Name of the custom term library')
            ->withResourceType('TEXT')
            ->withLibType('textKeyword')
            ->withCategory('BLACK')
            ->request();
        print_r($response->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;
    }
  • Create a text pattern library
    <?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();
    
        $response = Green::v20170823()->createKeywordLib()
            ->withServiceModule('open_api')
            ->withName ('Name of the text pattern library')
            ->withResourceType('TEXT')
            ->withLibType('similarText')
            ->withCategory('BLACK')
            ->request();
        print_r($response->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;
    }

Modify a text library

Change the name of a text library and the business scenario to which the text library applies.

<?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();

    $response = Green::v20170823()->updateKeywordLib()
        ->withId ('ID of the text library')
        ->withName('New name of the text library')
        ->request();
    print_r($response->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;
}

Delete a text library

Notice If you delete a text library, the text entries in the text library are also removed. You cannot delete a feedback-based text library.
<?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();

    $response = Green::v20170823()->deleteKeywordLib()
        ->withId ('ID of the text library')
        ->request();
    print_r($response->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;
}

Search for text entries

By default, the system returns all text entries in a text library by page. If you set the Keyword parameter, the system searches for all text entries that match the specified term in fuzzy mode.

<?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();

    $response = Green::v20170823()->describeKeyword()
        ->withKeywordLibId ('ID of the text library')
        ->request();
    print_r($response->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;
}

Add text entries

<?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();

    $response = Green::v20170823()->createKeyword()
        ->withKeywordLibId()
        ->withKeywords()
        ->request();
    print_r($response->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;
}

Remove text entries

<?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();

    $response = Green::v20170823()->deleteKeyword()
        ->withKeywordLibId ('ID of the text library')
        ->withIds('[\'Text ID_1\',\'Text ID_2\']')
        ->request();
    print_r($response->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;
}