使用PHP SDK语音反垃圾接口检测实时语音流或语音文件中的垃圾内容。

说明
  • 语音流检测和语音文件检测均为异步检测,检测结果需要您以轮询或者回调的方式获取。关于调用请求中的检测场景参数scene,返回结果中的分类参数label,以及操作建议参数suggestion的说明,请参见语音异步检测
  • 语音检测按照检测的语音文件/语音流的时间长度进行计费,计费粒度为分钟,每天累计检测总时长进行计量统计,每天检测总时长不足一分钟的按照1分钟进行计费。

准备工作

在进行具体的服务调用之前,请参考以下步骤,完成准备工作:
  1. 创建阿里云AccessKeyId和AccessKeySecret。具体请参考创建AccessKey
  2. 安装PHP依赖。具体请参考安装PHP依赖

提交语音异步检测任务

接口描述
接口 功能 支持的Region 描述
VoiceAsyncScanRequest 提交异步语音检测。 cn-shanghai
异步检测语音流或语音文件中是否包含违规内容。语音流格式支持:
  • rtmp
  • http
  • flv
提交语音URL示例代码
<?php

include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
use Green\Request\V20180509 as Green;

$iClientProfile = DefaultProfile::getProfile("cn-shanghai", "您自己的AccessKeyId", "您自己的AccessKeySecret");
DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
$client = new DefaultAcsClient($iClientProfile);
$request = new Green\VoiceAsyncScanRequest();
$request->setMethod("POST");
$request->setAcceptFormat("JSON");
$task1 = array('dataId' =>  uniqid(),
    'url' => 'http://xxxx.mp3',
);

/**
 * 语音垃圾内容检测: antispam
 **/

//如果是语音流检测,则live参数修改为true
$request->setContent(json_encode(array("tasks" => array($task1),
    "scenes" => array("antispam"), "live" => false)));
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;
                // 将taskId保存下来,间隔一段时间来轮询结果。
                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);
}
提交语音本地文件示例代码
<?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", "您自己的AccessKeyId", "您自己的AccessKeySecret");
DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
$client = new DefaultAcsClient($iClientProfile);
$request = new Green\VoiceAsyncScanRequest();
$request->setMethod("POST");
$request->setAcceptFormat("JSON");
$uploader = ClientUploader::getVoiceClientUploader($client);
$url = $uploader->uploadFile("d:/暴恐涉政1.mp4");
// 如果是语音文件检测 type => file
$task1 = array('dataId' =>  uniqid(),
    'url' => url,
    'type' => 'file'
);

/**
 * 语音垃圾内容检测: antispam
 **/
$request->setContent(json_encode(array("tasks" => array($task1),
    "scenes" => array("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;
                // 将taskId保存下来,间隔一段时间来轮询结果。
                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);
}

查询语音异步检测结果

接口描述
接口 功能 支持的Region 描述
VoiceAsyncScanResultsRequest 查询异步语音检测结果。 cn-shanghai 查询语音检测的结果。
示例代码
<?php

include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
use Green\Request\V20180509 as Green;

function main() {
    $iClientProfile = DefaultProfile::getProfile("cn-shanghai", "您的accessKeyId", "您的accessKeySecret");
    DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
    $client = new DefaultAcsClient($iClientProfile);
    $task_id = 'vc_f_1Puve6PmXbQ4DILWgDlOs8-1q5pby';

    if (strlen($task_id) > 0) {
        while (true) {
            $code = get_task($client, $task_id);
            if ($code == 280) {
                print_r("Scanning\n");
                sleep(10);
            } else if ($code == 200) {
                print_r("=====SUCCESS=====");
                break;
            } else {
                print_r("=====ERROR=====");
                break;
            }
        }
    }
}

function get_task($client, $task_id) {
    $request = new Green\VoiceAsyncScanResultsRequest();
    $request->setMethod("POST");
    $request->setAcceptFormat("JSON");

    $request->setContent(json_encode(array($task_id)));
    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;
                        print_r($scene);
                        print_r($suggestion);
                    }
                }else{
                    print_r("task process fail:" + $response->code);
                }
                return $taskResult->code;
            }
        }else{
            print_r("detect not success. code:" + $response->code);
            return $response->code;
        }
    } catch (Exception $e) {
        print_r($e);
    }
    return null;
}


main();