PHP實(shí)現(xiàn)文本審核的方法-創(chuàng)新互聯(lián)

本篇文章為大家展示了PHP實(shí)現(xiàn)文本審核的方法,代碼簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

“專業(yè)、務(wù)實(shí)、高效、創(chuàng)新、把客戶的事當(dāng)成自己的事”是我們每一個人一直以來堅(jiān)持追求的企業(yè)文化。 創(chuàng)新互聯(lián)是您可以信賴的網(wǎng)站建設(shè)服務(wù)商、專業(yè)的互聯(lián)網(wǎng)服務(wù)提供商! 專注于成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、軟件開發(fā)、設(shè)計(jì)服務(wù)業(yè)務(wù)。我們始終堅(jiān)持以客戶需求為導(dǎo)向,結(jié)合用戶體驗(yàn)與視覺傳達(dá),提供有針對性的項(xiàng)目解決方案,提供專業(yè)性的建議,創(chuàng)新互聯(lián)建站將不斷地超越自我,追逐市場,引領(lǐng)市場!

步驟:

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

PHP實(shí)現(xiàn)文本審核的方法

注冊賬號,進(jìn)入控制臺

PHP實(shí)現(xiàn)文本審核的方法

創(chuàng)建自己的應(yīng)用,獲取apikey 和秘鑰

PHP實(shí)現(xiàn)文本審核的方法

進(jìn)入文檔頁 文本審核:

PHP實(shí)現(xiàn)文本審核的方法

圖像審核:

PHP實(shí)現(xiàn)文本審核的方法

代碼實(shí)例:

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);//抓取指定網(wǎng)頁
    curl_setopt($curl, CURLOPT_HEADER, 0);//設(shè)置header
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求結(jié)果為字符串且輸出到屏幕上
    curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
    curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
    $data = curl_exec($curl);//運(yùn)行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);
    // 要求結(jié)果為字符串
    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);
  }
}

上述內(nèi)容就是PHP實(shí)現(xiàn)文本審核的方法,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

分享題目:PHP實(shí)現(xiàn)文本審核的方法-創(chuàng)新互聯(lián)
鏈接URL:http://muchs.cn/article22/djgpjc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、網(wǎng)站改版、ChatGPT、外貿(mào)建站、品牌網(wǎng)站建設(shè)小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)