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

Background information

Content Moderation SDK for PHP supports both synchronous and asynchronous video moderation.
  • If you use synchronous video moderation, you can submit only a sequence of frames captured from a video for moderation. For more information about parameters, see Synchronous moderation.
  • If you use asynchronous video moderation, you can submit a video or a sequence of frames captured from the video for moderation. We recommend that you use asynchronous video moderation. 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 asynchronous video moderation tasks (Recommended)

Operation Description Supported region
VideoAsyncScanRequest Sends asynchronous requests to moderate videos for risky content in multiple moderation scenarios, including pornography, terrorist content, ad, 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 video to be moderated',
        'url' => 'URL of the video to be moderated',
    );

    // scenes: the moderation scenarios. You can specify one or more moderation scenarios. 
    // callback and seed: optional. The two parameters are used to configure a callback notification. 
    $result = Green::v20180509()->videoAsyncScan()
        ->body(json_encode(array("tasks" => array($task1),
            "scenes" => array("porn", "terrorism"),
            'callback' => 'Callback URL',
            'seed' => 'Random string')))
        ->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 asynchronous video moderation results

Operation Description Supported region
VideoAsyncScanResultsRequest Queries asynchronous video moderation results.
Note Instead of calling this operation to poll the moderation results, we recommend that you set the callback parameter when you submit asynchronous video moderation tasks to receive the moderation results.
  • 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()->videoAsyncScanResults()
        ->body(json_encode(array('ID of the video 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;
}

Submit synchronous video moderation tasks

Operation Description Supported region
VideoSyncScanRequest Sends synchronous requests to moderate videos for risky content.
Note You can submit only a sequence of frames that are captured from a video for video moderation. To submit other types of videos, we recommend that you use the VideoAsyncScanRequest operation.
  • 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('frames' => array(['offset' => '0', 'url'=> 'URL of Video Frame 1'],
        ['offset' => '1', 'url' => 'URL of Video Frame 2'],
        ['offset' => '2', 'url' => 'URL of Video Frame 3']));

    // scenes: the moderation scenarios. You can specify one or more moderation scenarios. 
    $result = Green::v20180509()->videoSyncScan()
        ->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;
}

Provide feedback on moderation results

If the moderation results are not as expected, you can call the VideoFeedbackRequest operation to modify the results. Content Moderation adds the moderated video frames to the similar image blacklist or whitelist based on your feedback. When you submit a similar video frame 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
VideoFeedbackRequest Provides feedback on video moderation results and modifies the machine-assisted moderation results based on the feedback.
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($ak['accessKeyId'], $ak['accessKeySecret'])
        ->regionId('cn-shanghai')
        ->asDefaultClient();

    // scenes: the moderation scenarios. You can specify one or more moderation scenarios. 
    // suggestion: the moderation result that you expect to return. A value of pass indicates that the moderated video is normal. A value of block indicates that the moderated video contains violations. 
    $task1 = array("taskId" => 'ID of the video moderation task',
        "dataId" => "ID of the video to be moderated",
        "url" => "URL of the video to be moderated",
        "frames" => array(array("url" => "URL of Video Frame 1", "offset" => "100"), array("url" => "URL of Video Frame 2", "offset" => "200")),
        "suggestion" => 'block',
        "scenes" => array("ad", "terrorism"),
        "note" => "Remarks");

    $result = Green::v20180509()->videoFeedback()
        ->body(json_encode($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;
}