本文介绍了如何使用PHP SDK管理自定义文本库,以满足文本反垃圾检测场景的个性化需求。

功能描述

根据文本类型的不同,文本库分为关键词文本库和相似文本文本库;根据管控目的不同,文本库分为白名单、黑名单、疑似名单。关于参数的详细信息,请参见自定义文本库

关于该API的接入地址,请参见接入地址(Endpoint)

前提条件

已安装PHP依赖。关于安装PHP依赖的具体操作,请参见安装PHP依赖

查询文本库列表

  • 查询关键词文本库列表
    <?php
    
    include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
    
    use Green\Request\V20170823 as Green;
    
    // 请替换成您的AccessKey ID、AccessKey Secret。
    $iClientProfile = DefaultProfile::getProfile("cn-shanghai", "您的AccessKey ID", "您的AccessKey Secret");
    DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
    $client = new DefaultAcsClient($iClientProfile);
    
    $request = new Green\DescribeKeywordLibRequest();
    $request->setMethod("POST");
    $request->setAcceptFormat("JSON");
    $request->setServiceModule("open_api");
    
    try {
        $response = $client->getAcsResponse($request);
        print_r($response);
        $allLibs = $response->KeywordLibList;
        $textAntispamKeywordLibs = array();
        foreach ($allLibs as $keywordLib) {
            $libType = $keywordLib->LibType;
            $resourceType = $keywordLib->ResourceType;
            $source = $keywordLib->Source;
            # 查询自定义的关键词文本库。
            if ("textKeyword" == $libType and "TEXT" == $resourceType and "MANUAL" == $source) {
                array_push($textAntispamKeywordLibs, $keywordLib);
            }
            # 查询回流的关键词文本库。
            if ("textKeyword" == $libType and "TEXT" == $resourceType and "FEEDBACK" == $source) {
                array_push($textAntispamKeywordLibs, $keywordLib);
            }
        }
        print_r($textAntispamKeywordLibs);
    } catch (Exception $e) {
        print_r($e);
    }
  • 查询相似文本库列表(包含自定义和系统回流的相似文本库)
    <?php
    
    include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
    
    use Green\Request\V20170823 as Green;
    
    // 请替换成您的AccessKey ID、AccessKey Secret。
    $iClientProfile = DefaultProfile::getProfile("cn-shanghai", "您的AccessKey ID", "您的AccessKey Secret");
    DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
    $client = new DefaultAcsClient($iClientProfile);
    
    $request = new Green\DescribeKeywordLibRequest();
    $request->setMethod("POST");
    $request->setAcceptFormat("JSON");
    $request->setServiceModule("open_api");
    
    try {
        $response = $client->getAcsResponse($request);
        print_r($response);
        $allLibs = $response->KeywordLibList;
        $similarTextLibs = array();
        foreach ($allLibs as $keywordLib) {
            $libType = $keywordLib->LibType;
            $resourceType = $keywordLib->ResourceType;
            $source = $keywordLib->Source;
            # 查询自定义的关键词文本库。
            if ("similarText" == $libType and "TEXT" == $resourceType and "MANUAL" == $source) {
                array_push($similarTextLibs, $keywordLib);
            }
            # 查询回流的关键词文本库。
            if ("similarText" == $libType and "TEXT" == $resourceType and "FEEDBACK" == $source) {
                array_push($similarTextLibs, $keywordLib);
            }
        }
        print_r($similarTextLibs);
    } catch (Exception $e) {
        print_r($e);
    }

创建文本库

  • 创建关键词文本库
    <?php
    
    include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
    
    use Green\Request\V20170823 as Green;
    
    // 请替换成您的AccessKey ID、AccessKey Secret。
    $iClientProfile = DefaultProfile::getProfile("cn-shanghai", "您的AccessKey ID", "您的AccessKey Secret");
    DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
    $client = new DefaultAcsClient($iClientProfile);
    
    $request = new Green\CreateKeywordLibRequest();
    $request->setMethod("POST");
    $request->setAcceptFormat("JSON");
    $request->setServiceModule("open_api");
    $request->setName("自定义的关键词文本库");
    $request->setResourceType("TEXT");
    $request->setLibType("textKeyword");
    $request->setCategory("BLACK");
    try {
        $response = $client->getAcsResponse($request);
        print_r($response);
    } catch (Exception $e) {
        print_r($e);
    }
  • 创建相似文本库
    <?php
    
    include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
    
    use Green\Request\V20170823 as Green;
    
    // 请替换成您的AccessKey ID、AccessKey Secret。
    $iClientProfile = DefaultProfile::getProfile("cn-shanghai", "您的AccessKey ID", "您的AccessKey Secret");
    DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
    $client = new DefaultAcsClient($iClientProfile);
    
    $request = new Green\CreateKeywordLibRequest();
    $request->setMethod("POST");
    $request->setAcceptFormat("JSON");
    $request->setServiceModule("open_api");
    $request->setName("自定义的相似文本库");
    $request->setResourceType("TEXT");
    $request->setLibType("similarText");
    $request->setCategory("BLACK");
    try {
        $response = $client->getAcsResponse($request);
        print_r($response);
    } catch (Exception $e) {
        print_r($e);
    }

修改文本库

更新文本库库名以及修改文本库所适用的业务场景(BizType)。

<?php

include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';

use Green\Request\V20170823 as Green;

// 请替换成您的AccessKey ID、AccessKey Secret。
$iClientProfile = DefaultProfile::getProfile("cn-shanghai", "您的AccessKey ID", "您的AccessKey Secret");
DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
$client = new DefaultAcsClient($iClientProfile);

$request = new Green\UpdateKeywordLibRequest();
$request->setId("7534001");
$request->setName("修改自定义文本库的名称");
try {
    $response = $client->getAcsResponse($request);
    print_r($response);
} catch (Exception $e) {
    print_r($e);
}

删除文本库

注意 删除文本库也将删除文本库下的文本。系统回流的文本库不允许删除。
<?php

include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';

use Green\Request\V20170823 as Green;

// 请替换成您的AccessKey ID、AccessKey Secret。
$iClientProfile = DefaultProfile::getProfile("cn-shanghai", "您的AccessKey ID", "您的AccessKey Secret");
DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
$client = new DefaultAcsClient($iClientProfile);

$request = new Green\DeleteKeywordLibRequest();
$request->setId("3353");
try {
    $response = $client->getAcsResponse($request);
    print_r($response);
} catch (Exception $e) {
    print_r($e);
}

查找文本

默认分页查询所有文本。如果设置了Keyword字段,将模糊查找包含该字段值的文本。

<?php

include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';

use Green\Request\V20170823 as Green;

// 请替换成您的AccessKey ID、AccessKey Secret。
$iClientProfile = DefaultProfile::getProfile("cn-shanghai", "您的AccessKey ID", "您的AccessKey Secret");
DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
$client = new DefaultAcsClient($iClientProfile);

$request = new Green\DescribeKeywordRequest();
$request->setKeywordLibId("11395");
try {
    $response = $client->getAcsResponse($request);
    print_r($response);
} catch (Exception $e) {
    print_r($e);
}

添加文本

<?php

include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';

use Green\Request\V20170823 as Green;

// 请替换成您的AccessKey ID、AccessKey Secret。
$iClientProfile = DefaultProfile::getProfile("cn-shanghai", "您的AccessKey ID", "您的AccessKey Secret");
DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
$client = new DefaultAcsClient($iClientProfile);

$request = new Green\CreateKeywordRequest();
$request->setKeywordLibId("11395");
$request->setKeywords("['你好吗', '棒棒的']");
try {
    $response = $client->getAcsResponse($request);
    print_r($response);
} catch (Exception $e) {
    print_r($e);
}

删除文本

<?php

include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';

use Green\Request\V20170823 as Green;

// 请替换成您的AccessKey ID、AccessKey Secret。
$iClientProfile = DefaultProfile::getProfile("cn-shanghai", "您的AccessKey ID", "您的AccessKey Secret");
DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
$client = new DefaultAcsClient($iClientProfile);

$request = new Green\DeleteKeywordRequest();
$request->setKeywordLibId("11395");
$request->setIds("['1580917', '1580918']");
try {
    $response = $client->getAcsResponse($request);
    print_r($response);
} catch (Exception $e) {
    print_r($e);
}