当前位置:首页小程序 > 正文

根据微信公众平台的要求,小程序需要接入内容安全API(imgSecCheck、msgSecCheck、mediaCheckAsync)来识别和过滤用户生成内容(UGC)中的违规信息

作者:野牛程序员:2025-03-03 21:41:06小程序阅读 2104
根据微信公众平台的要求,小程序需要接入内容安全API(imgSecCheck、msgSecCheck、mediaCheckAsync)来识别和过滤用户生成内容(UGC)中的违规信息

根据微信公众平台的要求,小程序需要接入内容安全API(imgSecCheckmsgSecCheckmediaCheckAsync)来识别和过滤用户生成内容(UGC)中的违规信息。以下是如何接入这些接口的步骤和代码示例。

1. 接入内容安全API的准备工作

  • 登录微信公众平台:
    微信公众平台 的小程序后台获取 AppIDAppSecret

  • 获取 access_token:
    需要通过接口获取 access_token,用于调用内容安全API。


2. PHP 获取 access_token 示例

function getAccessToken($appId, $appSecret) {
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appId}&secret={$appSecret}";
    $response = file_get_contents($url);
    $result = json_decode($response, true);
    return $result['access_token'] ?? null;
}

// 示例
$appId = '你的AppID';
$appSecret = '你的AppSecret';
$accessToken = getAccessToken($appId, $appSecret);

3. 文本内容安全检测 (msgSecCheck)

接口地址: https://api.weixin.qq.com/wxa/msg_sec_check?access_token=ACCESS_TOKEN
用途: 用于检测用户提交的文本内容是否存在违规信息。

PHP代码示例:

function checkTextSecurity($text, $accessToken) {
    $url = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token={$accessToken}";

    $postData = json_encode([
        'content' => $text
    ], JSON_UNESCAPED_UNICODE);

    $options = [
        'http' => [
            'header'  => "Content-Type: application/json",
            'method'  => 'POST',
            'content' => $postData
        ]
    ];
    $context  = stream_context_create($options);
    $response = file_get_contents($url, false, $context);
    $result = json_decode($response, true);

    if ($result['errcode'] == 0) {
        return true;  // 内容安全
    } else {
        return false; // 存在违规
    }
}

// 示例调用
$text = "测试文本内容";
if (checkTextSecurity($text, $accessToken)) {
    echo "文本内容安全";
} else {
    echo "文本存在违规";
}

4. 图片内容安全检测 (imgSecCheck)

接口地址: https://api.weixin.qq.com/wxa/img_sec_check?access_token=ACCESS_TOKEN
用途: 用于检测用户上传的图片是否含有违规内容。

PHP代码示例:

function checkImageSecurity($imagePath, $accessToken) {
    $url = "https://api.weixin.qq.com/wxa/img_sec_check?access_token={$accessToken}";

    $fileData = new CURLFile(realpath($imagePath));
    $postData = ['media' => $fileData];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);

    $result = json_decode($response, true);

    if ($result['errcode'] == 0) {
        return true;  // 图片内容安全
    } else {
        return false; // 存在违规
    }
}

// 示例调用
$imagePath = 'uploads/test.jpg';
if (checkImageSecurity($imagePath, $accessToken)) {
    echo "图片内容安全";
} else {
    echo "图片存在违规";
}

5. 视频及音频异步检测 (mediaCheckAsync)

接口地址: https://api.weixin.qq.com/wxa/media_check_async?access_token=ACCESS_TOKEN
用途: 异步检测视频或音频内容。

PHP代码示例:

function checkMediaSecurityAsync($mediaUrl, $mediaType, $accessToken) {
    $url = "https://api.weixin.qq.com/wxa/media_check_async?access_token={$accessToken}";

    $postData = json_encode([
        'media_url' => $mediaUrl,
        'media_type' => $mediaType,  // 1:音频, 2:视频
        'version' => 2,
        'scene' => 1
    ], JSON_UNESCAPED_UNICODE);

    $options = [
        'http' => [
            'header'  => "Content-Type: application/json",
            'method'  => 'POST',
            'content' => $postData
        ]
    ];
    $context  = stream_context_create($options);
    $response = file_get_contents($url, false, $context);
    $result = json_decode($response, true);

    if ($result['errcode'] == 0) {
        return true;  // 已提交检测
    } else {
        return false; // 提交检测失败
    }
}

// 示例调用
$mediaUrl = 'https://yourdomain.com/uploads/test.mp4';
$mediaType = 2;  // 视频
if (checkMediaSecurityAsync($mediaUrl, $mediaType, $accessToken)) {
    echo "视频已提交检测";
} else {
    echo "视频检测提交失败";
}

6. 录屏和截图要求

  1. 录屏内容:

    • 访问接口的代码运行过程。

    • 接口返回的 errcode0 时的成功信息。

    • 各类型内容检测(文本、图片、音频、视频)的完整调用过程。

  2. 截图内容:

    • 小程序的相关页面截图(含UGC发布功能)。

    • 代码片段(含接口调用部分)的截图。


总结

  • 获取 access_token 并缓存。

  • 根据内容类型调用相应的安全检测接口。

  • 记录并展示接口的调用和返回信息。

  • 录屏保存接口返回值并提交审核。

这样可以满足微信的内容安全要求,避免小程序被滥用或审核不通过。


野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892
野牛程序员教少儿编程与信息学竞赛-微信|电话:15892516892
  • 根据微信公众平台的要求,小程序需要接入内容安全API(imgSecCheck、msgSecCheck、mediaCheckAsync)来识别和过滤用户生成内容(UGC)中的违规信息
  • 相关推荐

    最新推荐

    热门点击