This topic shows you how to use SDKs 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 image frames captured from a video for moderation.
  • If you use asynchronous video moderation, you can submit either a video or a sequence of image frames captured from the video for moderation. We recommend that you use asynchronous video moderation.

You can submit the URL of an online video, the URL of an on-premises video, a binary video stream, or a video live stream for video moderation.

Before you begin

Before you call operations, make the following preparations:

(Recommended) Submit asynchronous video moderation tasks

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
  • Submit the URL of an online video for video moderation
    <? php
    
    require_once 'aliyuncs/aliyun-oss-php-sdk/autoload.php';
    include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
    
    use Green\Request\V20180509 as Green;
    
    $iClientProfile = DefaultProfile::getProfile("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret");
    DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
    $client = new DefaultAcsClient($iClientProfile);
    $request = new Green\VideoAsyncScanRequest();
    $request->setMethod("POST");
    $request->setAcceptFormat("JSON");
    
    // The system charges you based on the moderation scenario that you specify.
    // You can send a request to moderate multiple videos at a time and specify multiple moderation scenarios for each video. The system charges you based on the number of frames captured from each video and the number of moderation scenarios for each video.
    // For example, if you moderate two videos for both pornography and terrorist content at a time, the total expense is calculated based on the following formula: Total expense = Number of frames captured from the two videos × (Unit price in the pornography detection scenario + Unit price in the terrorist content detection scenario).
    $task1 = array('dataId' =>  uniqid(),
        'url' => 'http://xxxx.mp4'
    );
    $request->setContent(json_encode(array("tasks" => array($task1),
        "scenes" => array("porn", "terrorism"))));
    try {
        $response = $client->getAcsResponse($request);
        print_r($response);
        if(200 == $response->code){
            $taskResults = $response->data;
            foreach ($taskResults as $taskResult) {
                if(200 == $taskResult->code){
                    $taskId = $taskResult->taskId;
                    // Save the task ID, which is used to poll the moderation results at the specified interval. For more information, see the description of the VideoAsyncScanResultsRequest operation.
                    print_r($taskId);
                }else{
                    print_r("task process fail:" + $response->code);
                }
            }
        }else{
            print_r("detect not success. code:" + $response->code);
        }
    } catch (Exception $e) {
        print_r($e);
    }
  • Submit the URL of an on-premises video for video moderation
    <? php
    
    require_once 'aliyuncs/aliyun-oss-php-sdk/autoload.php';
    include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
    
    use Green\Request\V20180509 as Green;
    use Green\Request\Extension\ClientUploader;
    
    $iClientProfile = DefaultProfile::getProfile("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret");
    DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
    $client = new DefaultAcsClient($iClientProfile);
    $request = new Green\VideoAsyncScanRequest();
    $request->setMethod("POST");
    $request->setAcceptFormat("JSON");
    
    // The system charges you based on the moderation scenario that you specify.
    // You can send a request to moderate multiple videos at a time and specify multiple moderation scenarios for each video. The system charges you based on the number of frames captured from each video and the number of moderation scenarios for each video.
    // For example, if you moderate two videos for both pornography and terrorist content at a time, the total expense is calculated based on the following formula: Total expense = Number of frames captured from the two videos × (Unit price in the pornography detection scenario + Unit price in the terrorist content detection scenario).
    // Upload your on-premises image before moderation.
    $uploader = ClientUploader::getVideoClientUploader($client);
    $url = $uploader->uploadFile("d:/Terrorist content1.mp4");
    $task1 = array('dataId' =>  uniqid(),
        'url' => $url
    );
    $request->setContent(json_encode(array("tasks" => array($task1),
        "scenes" => array("porn", "terrorism"))));
    try {
        $response = $client->getAcsResponse($request);
        print_r($response);
        if(200 == $response->code){
            $taskResults = $response->data;
            foreach ($taskResults as $taskResult) {
                if(200 == $taskResult->code){
                    $taskId = $taskResult->taskId;
                    // Save the task ID, which is used to poll the moderation results at the specified interval. For more information, see the description of the VideoAsyncScanResultsRequest operation.
                    print_r($taskId);
                }else{
                    print_r("task process fail:" + $response->code);
                }
            }
        }else{
            print_r("detect not success. code:" + $response->code);
        }
    } catch (Exception $e) {
        print_r($e);
    }
  • Submit a binary video stream for video moderation
    <? php
    
    require_once 'aliyuncs/aliyun-oss-php-sdk/autoload.php';
    include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
    
    use Green\Request\V20180509 as Green;
    use Green\Request\Extension\ClientUploader;
    
    $iClientProfile = DefaultProfile::getProfile("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret");
    DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
    $client = new DefaultAcsClient($iClientProfile);
    $request = new Green\VideoAsyncScanRequest();
    $request->setMethod("POST");
    $request->setAcceptFormat("JSON");
    
    // The system charges you based on the moderation scenario that you specify.
    // You can send a request to moderate multiple videos at a time and specify multiple moderation scenarios for each video. The system charges you based on the number of frames captured from each video and the number of moderation scenarios for each video.
    // For example, if you moderate two videos for both pornography and terrorist content at a time, the total expense is calculated based on the following formula: Total expense = Number of frames captured from the two videos × (Unit price in the pornography detection scenario + Unit price in the terrorist content detection scenario).
    $uploader = ClientUploader::getVideoClientUploader($client);
    $bytes = file_get_contents('d:/test.mp4');
    $url = $uploader->uploadBytes($bytes);
    $task1 = array('dataId' =>  uniqid(),
        'url' => $url
    );
    $request->setContent(json_encode(array("tasks" => array($task1),
        "scenes" => array("porn", "terrorism"))));
    try {
        $response = $client->getAcsResponse($request);
        print_r($response);
        if(200 == $response->code){
            $taskResults = $response->data;
            foreach ($taskResults as $taskResult) {
                if(200 == $taskResult->code){
                    $taskId = $taskResult->taskId;
                    // Save the task ID, which is used to poll the moderation results at the specified interval. For more information, see the description of the VideoAsyncScanResultsRequest operation.
                    print_r($taskId);
                }else{
                    print_r("task process fail:" + $response->code);
                }
            }
        }else{
            print_r("detect not success. code:" + $response->code);
        }
    } catch (Exception $e) {
        print_r($e);
    }
  • Submit a video live stream for video moderation
    <? php
    
    require_once 'aliyuncs/aliyun-oss-php-sdk/autoload.php';
    include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
    
    use Green\Request\V20180509 as Green;
    
    $iClientProfile = DefaultProfile::getProfile("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret");
    DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
    $client = new DefaultAcsClient($iClientProfile);
    $request = new Green\VideoAsyncScanRequest();
    $request->setMethod("POST");
    $request->setAcceptFormat("JSON");
    
    // The system charges you based on the moderation scenario that you specify.
    // You can send a request to moderate multiple video streams at a time and specify multiple moderation scenarios for each video stream. The system charges you based on the number of frames captured from each video stream and the number of moderation scenarios for each video stream.
    // For example, if you moderate two videos for both pornography and terrorist content at a time, the total expense is calculated based on the following formula: Total expense = Number of frames captured from the two videos × (Unit price in the pornography detection scenario + Unit price in the terrorist content detection scenario).
    $task1 = array('dataId' =>  uniqid(),
        'url' => 'http://URL of your video live stream.flv'
    );
    $request->setContent(json_encode(array("tasks" => array($task1),
        "scenes" => array("porn", "terrorism"), "live" => true)));
    try {
        $response = $client->getAcsResponse($request);
        print_r($response);
        if(200 == $response->code){
            $taskResults = $response->data;
            foreach ($taskResults as $taskResult) {
                if(200 == $taskResult->code){
                    $taskId = $taskResult->taskId;
                    // Save the task ID, which is used to poll the moderation results at the specified interval. For more information, see the description of the VideoAsyncScanResultsRequest operation.
                    print_r($taskId);
                }else{
                    print_r("task process fail:" + $response->code);
                }
            }
        }else{
            print_r("detect not success. code:" + $response->code);
        }
    } catch (Exception $e) {
        print_r($e);
    }
  • Submit a video live stream to moderate both the video images and audio
    <? php
    
    require_once 'aliyuncs/aliyun-oss-php-sdk/autoload.php';
    include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
    
    use Green\Request\V20180509 as Green;
    
    $iClientProfile = DefaultProfile::getProfile("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret");
    DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
    $client = new DefaultAcsClient($iClientProfile);
    $request = new Green\VideoAsyncScanRequest();
    $request->setMethod("POST");
    $request->setAcceptFormat("JSON");
    
    # Create a video moderation task. By default, you can only submit one task in a request. If you want to submit more than one task in a request, submit a ticket.
    # The system charges you based on the moderation scenario that you specify.
    # You can send a request to moderate multiple videos at a time and specify multiple moderation scenarios for each video. The system charges you based on the number of frames captured from each video and the number of moderation scenarios for each video.
    # For example, if you moderate two videos for both pornography and terrorist content at a time, the total expense is calculated based on the following formula: Total expense = Number of frames captured from the two videos × (Unit price in the pornography detection scenario + Unit price in the terrorist content detection scenario).
    # If you moderate both images and audio in a video, the expense for image moderation is calculated based on the preceding rule and that for audio moderation is calculated based on the audio duration.
    $task1 = array('dataId' =>  uniqid(),
        'url' => 'http://URL of the video.mp4'
    );
    $request->setContent(json_encode(array("tasks" => array($task1),
        "scenes" => array("porn", "terrorism"), "audioScenes" => 'antispam')));
    try {
        $response = $client->getAcsResponse($request);
        print_r($response);
        if(200 == $response->code){
            $taskResults = $response->data;
            foreach ($taskResults as $taskResult) {
                if(200 == $taskResult->code){
                    $taskId = $taskResult->taskId;
                    // Save the task ID, which is used to poll the moderation results at the specified interval. For more information, see the description of the VideoAsyncScanResultsRequest operation.
                    print_r($taskId);
                }else{
                    print_r("task process fail:" + $response->code);
                }
            }
        }else{
            print_r("detect not success. code:" + $response->code);
        }
    } catch (Exception $e) {
        print_r($e);
    }

Query the results of asynchronous video moderation

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
include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
use Green\Request\V20180509 as Green;

// Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account.
$iClientProfile = DefaultProfile::getProfile("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret");
DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
$client = new DefaultAcsClient($iClientProfile);
$request = new Green\VideoAsyncScanResultsRequest();
$request->setMethod("POST");
$request->setAcceptFormat("JSON");

// Specify the task ID returned after you submit the asynchronous moderation task.
$request->setContent(json_encode(array("testtaskid")));
try {
    $response = $client->getAcsResponse($request);
    print_r($response);
    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 on the image based on the values of the scene and suggestion parameters.
                    //do something
                }
            }else{
                print_r("task process fail:" + $response->code);
            }
        }
    }else{
        print_r("detect not success. code:" + $response->code);
    }
} catch (Exception $e) {
    print_r($e);
}

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

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

use Green\Request\V20180509 as Green;

$iClientProfile = DefaultProfile::getProfile("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret");
DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
$client = new DefaultAcsClient($iClientProfile);
$request = new Green\VideoSyncScanRequest();
$request->setMethod("POST");
$request->setAcceptFormat("JSON");

// The system charges you based on the moderation scenario that you specify.
// You can send a request to moderate multiple videos at a time and specify multiple moderation scenarios for each video. The system charges you based on the number of frames captured from each video and the number of moderation scenarios for each video.
// For example, if you moderate two videos for both pornography and terrorist content at a time, the total expense is calculated based on the following formula: Total expense = Number of frames captured from the two videos × (Unit price in the pornography detection scenario + Unit price in the terrorist content detection scenario).
$task1 = array('frames' => array(['offset' => '0', 'url'=>'https://test1.jpg'] ,
    ['offset' => '1', 'url'=>'https://test2.jpg'] ,
    ['offset' => '2', 'url'=>'https://test3.jpg'] ));

$request->setContent(json_encode(array("tasks" => array($task1),
    "scenes" => array("porn", "terrorism"))));
try {
    $response = $client->getAcsResponse($request);
    print_r($response);
    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 on the image based on the values of the scene and suggestion parameters.
                    //do something
                }
            }else{
                print_r("task process fail:" + $response->code);
            }
        }
    }
} catch (Exception $e) {
    print_r($e);
}