All Products
Search
Document Center

Content Moderation:Image moderation

Last Updated:Dec 29, 2025

This topic describes how to use Content Moderation SDK for PHP to moderate images for risky content.

Description

Image Moderation supports synchronous and asynchronous detection.

  • Synchronous detection provides real-time results. For more information about the parameters, see Synchronous detection.

  • If you use asynchronous image moderation, you must poll the moderation results or configure a callback notification to receive the moderation results. For more information about the related parameters, see /green/image/asyncscan and /green/image/results.

Note
  • This SDK supports only image URLs. It does not support local files or binary data.

  • The SDK supports public HTTP or HTTPS URLs that are a maximum of 2,048 characters in length.

Prerequisites

The PHP dependencies have been installed. For more information, see Install PHP dependencies.

Note

You must use the required PHP version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.

(Recommended) Synchronous Image Scan

Operation

Description

Supported region

ImageSyncScanRequest

Sends synchronous requests to moderate images for risky content in multiple moderation scenarios, including pornography, terrorist content, ad, QR code, undesirable scene, and logo detection.

  • cn-shanghai: China (Shanghai)

  • cn-beijing: China (Beijing)

  • cn-shenzhen: China (Shenzhen)

  • ap-southeast-1: Singapore

Sample code

<?php

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Green\Green;

try {
    /**
     * Note: Reuse the instantiated client as much as possible to improve detection performance. This avoids repeated connections.
     * Common ways 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('Obtain the AccessKey ID of the RAM user from an environment variable', 'Obtain the AccessKey secret of the RAM user from an environment variable')
        ->timeout(10) // The timeout period is 10 seconds. This setting applies to all requests that are sent using this client and do not have separate timeout settings.
        ->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 are sent using this client and do not have separate connection timeout settings.
        ->regionId('cn-shanghai')
        ->asDefaultClient();

    $task1 = array('dataId' => 'Business data ID',
        'url' => 'URL of the image to be moderated'
    );

    /* Set the images to be moderated. One image corresponds to one moderation task.
     * If you moderate multiple images at the same time, the processing time is determined by the time when the last image is moderated.
     * In most cases, the average response time for batch moderation is longer than that for single-image moderation. The more images you submit in a batch, the higher the probability that the response time is extended.
     * The sample code shows how to moderate a single image. To moderate multiple images in a batch, create multiple moderation tasks.
     * Optical character recognition (OCR) is billed based on the number of card or certificate images that are moderated multiplied by the unit price for each card or certificate type.
     */
    $response = Green::v20180509()->imageSyncScan()
        ->timeout(10) // The timeout period is 10 seconds. This setting applies only to 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. This setting applies only to the current request.
        ->body(json_encode(array('tasks' => array($task1), 'scenes' => array('porn'), 'bizType' => 'Business scenario')))
        ->request();
    print_r($response->toArray());
    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;
                    // Process the results based on the scene and suggestion.
                    print_r($scene);
                    print_r($suggestion);
                }
            } else {
                print_r("task process fail:" . $response->code);
            }
        }
    } else {
        print_r("detect not success. code:" . $response->code);
    }
} catch (Exception $exception) {
    echo $exception->getMessage() . PHP_EOL;
}

Submit asynchronous image moderation tasks

This section describes how to use Content Moderation SDK for PHP to submit asynchronous image moderation tasks. When you submit a request, you can configure a callback notification to receive the moderation results. Alternatively, you can call the ImageAsyncScanResultsRequest operation to poll the moderation results.

Operation

Description

Supported region

ImageAsyncScanRequest

Sends asynchronous requests to moderate images for risky content in multiple moderation scenarios, including pornography, terrorist content, ad, QR code, undesirable scene, and logo detection.

  • cn-shanghai: China (Shanghai)

  • cn-beijing: China (Beijing)

  • cn-shenzhen: China (Shenzhen)

  • ap-southeast-1: Singapore

Sample code

<?php

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

try {
     /**
     * Note: Reuse the instantiated client as much as possible to improve detection performance. This avoids repeated connections.
     * Common ways 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('Obtain the AccessKey ID of the RAM user from an environment variable', 'Obtain the AccessKey secret of the RAM user from an environment variable')
        ->timeout(10) // The timeout period is 10 seconds. This setting applies to all requests that are sent using this client and do not have separate timeout settings.
        ->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 are sent using this client and do not have separate connection timeout settings.
        ->regionId('cn-shanghai')
        ->asDefaultClient();

    $task1 = array('dataId' => 'Business data ID',
        'url' => 'URL of the image to be moderated'
    );

    // Example: porn and terrorism.
    $result = Green::v20180509()->imageAsyncScan()
        ->timeout(10) // The timeout period is 10 seconds. This setting applies only to 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. This setting applies only to the current request.
        ->body(json_encode(array("tasks" => array($task1), "scenes" => array("porn", "terrorism"), "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;
}

Query the results of asynchronous image moderation

Operation

Description

Supported region

ImageAsyncScanResultsRequest

Queries asynchronous image moderation results. You can query the moderation results of multiple asynchronous image moderation tasks at a time.

  • cn-shanghai: China (Shanghai)

  • cn-beijing: China (Beijing)

  • cn-shenzhen: China (Shenzhen)

  • ap-southeast-1: Singapore

Sample code

<?php

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

try {
     /**
     * Note: Reuse the instantiated client as much as possible to improve detection performance. This avoids repeated connections.
     * Common ways 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('Obtain the AccessKey ID of the RAM user from an environment variable', 'Obtain the AccessKey secret of the RAM user from an environment variable')
        ->timeout(10) // The timeout period is 10 seconds. This setting applies to all requests that are sent using this client and do not have separate timeout settings.
        ->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 are sent using this client and do not have separate connection timeout settings.
        ->regionId('cn-shanghai')
        ->asDefaultClient();

    $result = Green::v20180509()->imageAsyncScanResults()
        ->timeout(10) // The timeout period is 10 seconds. This setting applies only to 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. This setting applies only to the current request.
        ->body(json_encode(array('ID of the asynchronous image moderation task')))
        ->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;
}

Provide feedback on image moderation results

If the results of image moderation do not meet your expectations, you can call the ImageScanFeedbackRequest operation to modify the results. Content Moderation adds the moderated image to the similar image blacklist or whitelist based on your feedback. When you submit a similar image for moderation, Content Moderation returns moderation results based on the label in your feedback.

For more information, see Detection Result Feedback.

API

Description

Supported region

ImageScanFeedbackRequest

You can submit feedback to correct the algorithm's image detection results.

  • cn-shanghai: China (Shanghai)

  • cn-beijing: China (Beijing)

  • cn-shenzhen: China (Shenzhen)

  • ap-southeast-1: Singapore

Sample code

<?php

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

try {
     /**
     * Note: Reuse the instantiated client as much as possible to improve detection performance. This avoids repeated connections.
     * Common ways 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('Obtain the AccessKey ID of the RAM user from an environment variable', 'Obtain the AccessKey secret of the RAM user from an environment variable')
        ->timeout(10) // The timeout period is 10 seconds. This setting applies to all requests that are sent using this client and do not have separate timeout settings.
        ->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 are sent using this client and do not have separate connection timeout settings.
        ->regionId('cn-shanghai')
        ->asDefaultClient();

    // Example: porn and terrorism.
    $result = Green::v20180509()->imageScanFeedback()
        ->timeout(10) // The timeout period is 10 seconds. This setting applies only to 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. This setting applies only to the current request.
        ->body(json_encode(array("suggestion" => "block", "scenes" => array("porn", "terrorism"), "url" => "URL of the image to be moderated")))
        ->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;
}