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

Background information

Content Moderation SDK for PHP supports both synchronous and asynchronous image moderation.

  • If you use synchronous image moderation, the moderation results are returned in real time. For more information about parameters, see Synchronous moderation.
  • 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 parameters, see Asynchronous moderation.

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.

Submit synchronous image moderation tasks (Recommended)

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 (Singapore)
Sample code
<?php

use AlibabaCloud\Client\AlibabaCloud;
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();

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

    /* Create one task for each image to be moderated. 
     * If you moderate multiple images in a request, the total response time that the server spends on processing the request starts from when the request is initiated to when the last image is moderated. 
     * Generally, the average response time of moderating multiple images in a request is longer than that of moderating a single image. The more images you submit at a time, the higher the probability that the average response time is extended. 
     * In this example, a single image is moderated. If you want to moderate multiple images at a time, create a task for each image to be moderated. 
     * The OCR expense equals the product of the number of card or certificate images moderated and the moderation unit price of the card or certificate images. 
     */
    $response = Green::v20180509()->imageSyncScan()
        ->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;
                    // Take a further action based on the values of the scene and suggestion parameters. 
                    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 shows you 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 (Singapore)
Sample code
<?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();

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

    // scenes: the moderation scenarios. Example: porn and terrorism. 
    $result = Green::v20180509()->imageAsyncScan()
        ->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 (Singapore)
Sample code
<?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();

    $result = Green::v20180509()->imageAsyncScanResults()
        ->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 moderation results

If the results of image moderation are not as expected, 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 Give feedback on moderation results.

Operation Description Supported region
ImageScanFeedbackRequest Provides feedback on image moderation results and modifies the machine-assisted moderation results based on the feedback.
  • cn-shanghai: China (Shanghai)
  • cn-beijing: China (Beijing)
  • cn-shenzhen: China (Shenzhen)
  • ap-southeast-1: Singapore (Singapore)
Sample code
<?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();

    // scenes: the moderation scenarios. Example: porn and terrorism. 
    $result = Green::v20180509()->imageScanFeedback()
        ->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;
}