File moderation

Updated at:

This topic describes how to use the PHP SDK file moderation API to detect text and images in files.

Features

File moderation supports only asynchronous detection tasks. These tasks do not return results in real time. For more information about the parameters, see Asynchronous file detection.

Note
  • The SDK supports only publicly accessible file URLs. It does not support local files or binary data.

  • Supported URL types:

    • Public HTTP/HTTPS URLs that do not exceed 2048 characters in length.

    • Alibaba Cloud OSS URLs: oss://<bucket-name>.<endpoint>/<object-name>. You must grant AI Guardrails permissions to access the destination OSS bucket. The bucket must be in the same region as the AI Guardrails service. For more information, see Grant AI Guardrails permissions to access OSS buckets.

Prerequisites

Before you begin, make sure you have:

  • Installed PHP dependencies using the required PHP version. Using a different version causes operation calls to fail. See Install PHP dependencies.

  • An Alibaba Cloud RAM user with AccessKey ID and AccessKey secret stored as environment variables:

    • ALIBABA_CLOUD_ACCESS_KEY_ID

    • ALIBABA_CLOUD_ACCESS_KEY_SECRET

Submit an asynchronous file detection task

API

Description

Supported regions

FileAsyncScanV2Request

Submits an asynchronous file detection task to detect text and images in a file.

  • cn-shanghai: China (Shanghai)

  • cn-beijing: China (Beijing)

Sample code

<?php

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

try {
    /**
     * Note: To improve detection performance, reuse the instantiated client and avoid repeatedly establishing connections.
     * Common way to obtain environment variables:
     * Obtain the AccessKey ID of a RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
     * Obtain the AccessKey secret of a RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
     */
    AlibabaCloud::accessKeyClient('Your RAM user AccessKey ID', 'Your RAM user AccessKey secret')
        ->timeout(10) // The timeout period is 10 seconds. This setting applies to all requests that use this client and do not have a separately specified timeout period.
        ->connectTimeout(3) // The connection timeout period is 3 seconds. If the value is less than 1, the unit is automatically converted to milliseconds. This setting applies to all requests that use this client and do not have a separately specified connection timeout period.
        ->regionId('cn-shanghai')
        ->asDefaultClient();

    $task1 = array('dataId' => 'Data ID for detection',
        'url' => 'URL of the file to be detected'
    );

    /**
     * textScenes: If the content to be detected contains text, specify the detection scenario. Valid value: antispam.
     * imageScenes: If the content to be detected contains images, specify the detection scenario.
     */
    $result = Green::v20180509()->fileAsyncScan()
        ->timeout(10) // The timeout period is 10 seconds. The request timeout setting is valid only for the current request.
        ->connectTimeout(3) // The connection timeout period is 3 seconds. If the value is less than 1, the unit is automatically converted to milliseconds. The request timeout setting is valid only for the current request.
        ->body(json_encode(array(
            'bizType' => 'Business scenario',
            'textScenes' => array('antispam'),
            'imageScenes' => array('porn', 'ad'),
            'tasks' => array($task1)
        )))
        ->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;
}

Query the results of an asynchronous file detection task

API

Description

Supported regions

FileAsyncScanResultsRequest

Queries the results of an asynchronous file detection task.

  • cn-shanghai: China (Shanghai)

  • cn-beijing: China (Beijing)

Sample code

<?php

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

try {
    /**
     * Note: To improve detection performance, reuse the instantiated client and avoid repeatedly establishing connections.
     * Common way to obtain environment variables:
     * Obtain the AccessKey ID of a RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
     * Obtain the AccessKey secret of a RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
     */
    AlibabaCloud::accessKeyClient('Your RAM user AccessKey ID', 'Your RAM user AccessKey secret')
        ->timeout(10) // The timeout period is 10 seconds. This setting applies to all requests that use this client and do not have a separately specified timeout period.
        ->connectTimeout(3) // The connection timeout period is 3 seconds. If the value is less than 1, the unit is automatically converted to milliseconds. This setting applies to all requests that use this client and do not have a separately specified connection timeout period.
        ->regionId('cn-shanghai')
        ->asDefaultClient();

    $result = Green::v20180509()->fileAsyncScanResults()
        ->timeout(10) // The timeout period is 10 seconds. The request timeout setting is valid only for the current request.
        ->connectTimeout(3) // The connection timeout period is 3 seconds. If the value is less than 1, the unit is automatically converted to milliseconds. The request timeout setting is valid only for the current request.
        ->body(json_encode(array('File moderation task ID')))
        ->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;
}