国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP利用百度ai實現文本和圖片審核

2024-05-05 00:08:57
字體:
來源:轉載
供稿:網友

之前做平臺內容發布審核都是自己構建一套違禁詞庫,在代碼中利用詞庫判斷用戶發布的內容,現在可以使用百度ai api完成這個功能。接下來就簡單說下怎么做吧:

首先打開百度ai 開發平臺 注冊一個賬號:

PHP,百度ai,文本,圖片,審核

注冊賬號,進入控制臺

PHP,百度ai,文本,圖片,審核

創建自己的應用,獲取apikey 和秘鑰 

PHP,百度ai,文本,圖片,審核

進入文檔頁 文本審核:

PHP,百度ai,文本,圖片,審核

圖像審核:

PHP,百度ai,文本,圖片,審核

文檔很詳細,實現用戶發布內容審核 圖片審核還是很方便簡單的。我沒有使用官方的sdk,簡單的整合了一下作為練手,以下是我簡單的代碼demo:

class Sentive{  protected $accessTokenUrl = 'https://aip.baidubce.com/oauth/2.0/token';//獲取token url  protected $textUrl = 'https://aip.baidubce.com/rest/2.0/antispam/v2/spam';//文本審核url  protected $imgUrl = 'https://aip.baidubce.com/api/v1/solution/direct/img_censor';//圖片審核url  protected $avatarUrl = 'https://aip.baidubce.com/rest/2.0/solution/v1/face_audit';//頭像審核url  protected $grant_type;  protected $client_id;  protected $client_secret;  function __construct()  {    $this->grant_type = 'client_credentials';    $this->client_id = 'xxx';//API Key    $this->client_secret = 'xxx';//Secret Key  }  static function request($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;  }  static function request_post($url = '', $param = array(), $type)  {    if (empty($url) || empty($param)) {      return false;    }    $postUrl = $url;    $curlPost = $param;    $curl = curl_init();    curl_setopt($curl, CURLOPT_URL, $postUrl);    curl_setopt($curl, CURLOPT_HEADER, 0);    // 要求結果為字符串    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);    // post方式    curl_setopt($curl, CURLOPT_POST, 1);    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);    curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);    if ($type == "text") {      curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));    } else {      curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=utf-8'));    }    curl_setopt($curl, CURLINFO_HEADER_OUT, true);    $data = curl_exec($curl);    $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);    if ($code === 0) {      throw new /Exception(curl_error($curl));    }    curl_close($curl);    return $data;  }  //獲取token  public function getToken()  {    new Redis();    $post_data['grant_type'] = $this->grant_type;    $post_data['client_id'] = $this->client_id;    $post_data['client_secret'] = $this->client_secret;    $o = "";    foreach ($post_data as $k => $v) {      $o .= "$k=" . urlencode($v) . "&";    }    $post_data = substr($o, 0, -1);    $res = self::request($this->accessTokenUrl, $post_data);    $redis->setkey("filterToken", json_decode($res, true)['access_token']);    return json_decode($res, true)['access_token'];  }  //文本審核  public function textVerify($data)  {    new Redis();    $token = $redis->get("filterToken");    if (empty($token)) {      $token = $this->getToken();    }    $curl = $this->textUrl . "?access_token=" . $token;    $result = self::request_post($curl, $data, "text");    return json_decode($result, true);  }  //圖片審核  public function imgVerify($img)  {    $redis = new Redis();    $token = $redis->get("filterToken");    if (empty($token)) {      $token = $this->getToken();    }    $curl = $this->imgUrl . "?access_token=" . $token;    $bodys = array(      'image' => $img,      'scenes' => array("ocr",        "face", "public", "politician", "antiporn", "terror", "webimage", "disgust",        'watermark')    );    $bodys = json_encode($bodys);    $result = self::request_post($curl, $bodys, "img");    return json_decode($result, true);  }  //頭像審核  public function avatarVerify($img)  {    $redis = new Redis();    $token = $redis->get("filterToken");    if (empty($token)) {      $token = $this->getToken();    }    $curl = $this->avatarUrl . "?access_token=" . $token;    $bodys = array(      "configId" => "1",      "images" => $img    );    $result = self::request_post($curl, $bodys, "text");    return json_decode($result, true);  }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 涞水县| 师宗县| 临潭县| 云阳县| 孝义市| 巴里| 沾化县| 宣化县| 岳普湖县| 中江县| 兴城市| 宝兴县| 石景山区| 文山县| 武强县| 江阴市| 大埔县| 五华县| 娄烦县| 博湖县| 呈贡县| 中牟县| 界首市| 新晃| 卢氏县| 晋江市| 栾城县| 庐江县| 富源县| 安塞县| 山西省| 黄石市| 乌什县| 东丰县| 固镇县| 中阳县| 禹州市| 霸州市| 老河口市| 抚顺县| 大邑县|