利用PHP和百度ai实现文本以及图片的审核-php教程

资源魔 93 0

步骤:

起首关上baiduai 开发平台 注册一个账号:

36303be188ef2cbec39e8ae627b3f3f.png

注册账号,进入管制台

20adaab428076df7b4522bc466b88f2.png

创立本人的使用,猎取apikey 以及秘钥

aa708dfd8897a685baf58cc2ff31e1e.png

进入文档页 文本审核:

ed6aa1370ef0b827b21a69a6939aba6.png

图象审核:

c71d0e16741940cbab0d4e73f404e85.png

代码实例:

class Sentive
{
  protected $accessTokenUrl = 'https://aip.百度bce.com/oauth/2.0/token';//猎取token url
  protected $textUrl = 'https://aip.百度bce.com/rest/2.0/antispam/v2/spam';//文本审核url
  protected $imgUrl = 'https://aip.百度bce.com/api/v1/solution/direct/img_censor';//图片审核url
  protected $avatarUrl = 'https://aip.百度bce.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);
  }
}

保举教程:PHP教程

以上就是行使PHP以及baiduai完成文本和图片的审核的具体内容,更多请存眷资源魔其它相干文章!

标签: php 图片 php开发教程 php开发资料 php开发自学 百度ai 文本 审核

抱歉,评论功能暂时关闭!