hi,你好!欢迎访问本站!登录
本站由网站地图腾讯云宝塔系统阿里云强势驱动
当前位置:首页 - 教程 - 后端开发 - 正文 君子好学,自强不息!

【后端开辟】应用PHP和百度ai完成文本以及图片的考核

2019-11-16后端开发搜奇网71°c
A+ A-

步骤:

起首翻开百度ai 开辟平台 注册一个账号:

注册账号,进入控制台

建立本身的运用,猎取apikey 和秘钥

进入文档页 文本考核:

图象考核:

代码实例:

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);
  }
}

引荐教程:PHP教程

以上就是应用PHP和百度ai完成文本以及图片的考核的细致内容,更多请关注ki4网别的相干文章!

  选择打赏方式
微信赞助

打赏

QQ钱包

打赏

支付宝赞助

打赏

  移步手机端
【后端开辟】应用PHP和百度ai完成文本以及图片的考核

1、打开你手机的二维码扫描APP
2、扫描左则的二维码
3、点击扫描获得的网址
4、可以在手机端阅读此文章

本文来源:搜奇网

本文地址:https://www.sou7.cn/281717.html

关注我们:微信搜索“搜奇网”添加我为好友

版权声明: 本文仅代表作者个人观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。请记住本站网址https://www.sou7.cn/搜奇网。

发表评论

选填

必填

必填

选填

请拖动滑块解锁
>>