wordpress评论上传的图片增加百度图片内容审核功能

使用说明:评论内如果有[img=***]标签,进行网址审核后返回结果。

1.百度api调用文件,复制以下代码,保存文件名baidu-imgcensor.php

<?php
function request_access_token($url = '', $param = '') {
        if (empty($url) || empty($param)) {
            return false;
        }
        
        $postUrl = $url;
        $curlPost = $param;
        $curl = curl_init();//初始化curl
        curl_setopt($curl, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($curl, CURLOPT_HEADER, 0);//设置header
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($curl);//运行curl
        curl_close($curl);
        
        return $data;
    }

	$url = 'https://aip.baidubce.com/oauth/2.0/token';
    $post_data['grant_type']       = 'client_credentials';
    $post_data['client_id']      = '你的 Api Key';
    $post_data['client_secret'] = '你的 Secret Key';
    $o = "";
    foreach ( $post_data as $k => $v ) 
    {
    	$o.= "$k=" . urlencode( $v ). "&" ;
    }
    $post_data = substr($o,0,-1);
    
    $res = request_access_token($url, $post_data);
$access_result = json_decode($res);
$access_result_text = $access_result->access_token;
//	print_r($pc_result_text);


/**
 * 发起http post请求(REST API), 并获取REST请求的结果
 * @param string $url
 * @param string $param
 * @return - http response body if succeeds, else false.
 */
function request_post($url = '', $param = '')
{
    if (empty($url) || empty($param)) {
        return false;
    }

    $postUrl = $url;
    $curlPost = $param;
    // 初始化curl
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $postUrl);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    // 要求结果为字符串且输出到屏幕上
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    // post提交方式
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
    // 运行curl
    $data = curl_exec($curl);
    curl_close($curl);

    return $data;
}
$token = $access_result->access_token;

2.主题目录下的functions.php文件内添加以下代码

部分主题需要把审核代码放入评论审核

/**
 * 百度api图片审核评论内容 https://www.77nn.net/6062.html
 */
require_once 'baidu-imgcensor.php';
$url = 'https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined?access_token=' . $token;
preg_match_all("|\[img=(.*)\\]|isU",$posts,$myarr);
foreach ($myarr[1] as $value)
{
	$img = file_get_contents($value);
$img = base64_encode($img);
$bodys = array(
    'image' => $img
);
$res1 = request_post($url, $bodys);
$img_result = (array) json_decode($res1,true);
if( $img_result['conclusionType'] ==2 ){
$pc_result_text = $img_result['data'][0]['msg'];
echo (json_encode(array('error' => 1, 'ys' => 'danger', 'msg' => $pc_result_text)));
        exit();
}elseif($img_result['conclusionType'] ==3){
echo (json_encode(array('error' => 1, 'ys' => 'danger', 'msg' => '图片不合规,请更换后提交')));
        exit();
}elseif($img_result['conclusionType'] ==4){
echo (json_encode(array('error' => 1, 'ys' => 'danger', 'msg' => '审核失败,请重试')));
        exit();
}elseif($img_result['error_code']){
echo (json_encode(array('error' => 1, 'ys' => 'danger', 'msg' => $img_result['error_msg'])));
        exit();
}
}

以上代码根据主题不同,可能需要修改echo一行的输出内容。如审核结果返回2,不合规,部分主题需要使用如下格式:

err("评论内容" . $img_result['data'][0]['msg'] . ",请重新评论", 409);
conclusionType审核结果类型,可取值1、2、3、4,分别代表1:合规,2:不合规,3:疑似,4:审核失败

官方参考文档https://ai.baidu.com/ai-doc/ANTIPORN/Vk42xcpu1

© 版权声明
THE END
喜欢就支持一下吧
点赞10赞赏 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容