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

2022-08-08 0 1,084

教程解读:

使用说明:评论内如果有[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 图片审核评论内容
 */
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:审核失败

教程结语:

如有不懂的可以@汇站

本文章已结束,如转载请注明:汇站网 » wordpress 评论上传的图片增加百度图片内容审核功能

收藏 (0)

微信支付 微信扫一扫

支付宝支付 支付宝扫一扫

打赏二维码
点赞 (0)

站长资源下载中心-找源码上汇站

常见问题
  • 如果付款后没有弹出下载页面,多刷新几下,有问题联系客服!
查看详情
  • 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
查看详情

相关文章

联系官方客服

为您解决烦忧 - 24小时在线 专业服务