当前位置: 首页 > 工具软件 > PHP-VCR > 使用案例 >

php 调用 百度SDK 实现 图片审核+文本内容审核(敏感词过滤)

上官迪
2023-12-01

1、注册百度智能云账号

2、控制台领取免费资源、创建内容审核应用

3、使用

<?php

namespace App\Http\Controllers;

use app\Request;

class Audit
{

    /**
     * 内容审核
     */
    public function contentAudit(Request $request)
    {
        $content = $request->input('content');
        $token = $this->getAccessToken('API Key', 'Secret Key');

        $url = 'https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token=' . $token;
        $bodys = array(
            'text' => $content
        );
        $res = $this->curlPost($url, $bodys);

        //结果转成数组
        $res = json_decode($res, true);
        //根据自己的业务逻辑进行处理
        print_r($res);die;
    }

    /**
     * 图片审核
     */
    public function imageAudit(Request $request)
    {
        $img = $request->file('img')->getPathname();
        $token = $this->getAccessToken('API Key', 'Secret Key');
        $url = 'https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined?access_token=' . $token;
        $img = file_get_contents($img);
        $img = base64_encode($img);
        $bodys = array(
            'image' => $img
        );
        $res = $this->curlPost($url, $bodys);
        //结果转成数组
        $res = json_encode($res, true);
        //根据自己的业务逻辑进行处理
        print_r($res);
    }

    /**
     * CURL的Post请求方法
     * @param string $url
     * @param string $param
     * @return bool|string
     */
    function curlPost($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;
    }

    /**
     * 获取百度开放平台的票据
     * 参考链接:https://ai.baidu.com/ai-doc/REFERENCE/Ck3dwjhhu
     */
    public function getAccessToken($ApiKey = '', $SecretKey = '', $grantType = 'client_credentials')
    {

        $url = 'https://aip.baidubce.com/oauth/2.0/token';
        $post_data['grant_type'] = $grantType;
        $post_data['client_id'] = $ApiKey;
        $post_data['client_secret'] = $SecretKey;
        $o = "";
        foreach ($post_data as $k => $v) {
            $o .= "$k=" . urlencode($v) . "&";
        }
        $post_data = substr($o, 0, -1);

        $res = $this->curlPost($url, $post_data);
        //进行把返回结果转成数组
        $res = json_decode($res, true);
        if (isset($res['error'])) {
            exit('API Key或者Secret Key不正确');
        }
        $accessToken = $res['access_token'];
        return $accessToken;
    }
}

4、route.php中设置路由进行调用即可

转载于:
作者:柯作
链接:https://juejin.cn/post/7003229996823085070
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 类似资料: