当前位置:Gxlcms > PHP教程 > 利用PHP和百度ai实现文本以及图片的审核

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

时间:2021-07-01 10:21:17 帮助过:17人阅读

步骤:

首先打开百度ai 开发平台 注册一个账号:

36303be188ef2cbec39e8ae627b3f3f.png

注册账号,进入控制台

20adaab428076df7b4522bc466b88f2.png

创建自己的应用,获取apikey 和秘钥

aa708dfd8897a685baf58cc2ff31e1e.png

进入文档页 文本审核:

ed6aa1370ef0b827b21a69a6939aba6.png

图像审核:

c71d0e16741940cbab0d4e73f404e85.png

代码实例:

  1. class Sentive
  2. {
  3. protected $accessTokenUrl = 'https://aip.baidubce.com/oauth/2.0/token';//获取token url
  4. protected $textUrl = 'https://aip.baidubce.com/rest/2.0/antispam/v2/spam';//文本审核url
  5. protected $imgUrl = 'https://aip.baidubce.com/api/v1/solution/direct/img_censor';//图片审核url
  6. protected $avatarUrl = 'https://aip.baidubce.com/rest/2.0/solution/v1/face_audit';//头像审核url
  7. protected $grant_type;
  8. protected $client_id;
  9. protected $client_secret;
  10. function __construct()
  11. {
  12. $this->grant_type = 'client_credentials';
  13. $this->client_id = 'xxx';//API Key
  14. $this->client_secret = 'xxx';//Secret Key
  15. }
  16. static function request($url = '', $param = '')
  17. {
  18. if (empty($url) || empty($param)) {
  19. return false;
  20. }
  21. $postUrl = $url;
  22. $curlPost = $param;
  23. $curl = curl_init();//初始化curl
  24. curl_setopt($curl, CURLOPT_URL, $postUrl);//抓取指定网页
  25. curl_setopt($curl, CURLOPT_HEADER, 0);//设置header
  26. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
  27. curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
  28. curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
  29. $data = curl_exec($curl);//运行curl
  30. curl_close($curl);
  31. return $data;
  32. }
  33. static function request_post($url = '', $param = array(), $type)
  34. {
  35. if (empty($url) || empty($param)) {
  36. return false;
  37. }
  38. $postUrl = $url;
  39. $curlPost = $param;
  40. $curl = curl_init();
  41. curl_setopt($curl, CURLOPT_URL, $postUrl);
  42. curl_setopt($curl, CURLOPT_HEADER, 0);
  43. // 要求结果为字符串
  44. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  45. // post方式
  46. curl_setopt($curl, CURLOPT_POST, 1);
  47. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  48. curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
  49. if ($type == "text") {
  50. curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
  51. } else {
  52. curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=utf-8'));
  53. }
  54. curl_setopt($curl, CURLINFO_HEADER_OUT, true);
  55. $data = curl_exec($curl);
  56. $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  57. if ($code === 0) {
  58. throw new \Exception(curl_error($curl));
  59. }
  60. curl_close($curl);
  61. return $data;
  62. }
  63. //获取token
  64. public function getToken()
  65. {
  66. new Redis();
  67. $post_data['grant_type'] = $this->grant_type;
  68. $post_data['client_id'] = $this->client_id;
  69. $post_data['client_secret'] = $this->client_secret;
  70. $o = "";
  71. foreach ($post_data as $k => $v) {
  72. $o .= "$k=" . urlencode($v) . "&";
  73. }
  74. $post_data = substr($o, 0, -1);
  75. $res = self::request($this->accessTokenUrl, $post_data);
  76. $redis->setkey("filterToken", json_decode($res, true)['access_token']);
  77. return json_decode($res, true)['access_token'];
  78. }
  79. //文本审核
  80. public function textVerify($data)
  81. {
  82. new Redis();
  83. $token = $redis->get("filterToken");
  84. if (empty($token)) {
  85. $token = $this->getToken();
  86. }
  87. $curl = $this->textUrl . "?access_token=" . $token;
  88. $result = self::request_post($curl, $data, "text");
  89. return json_decode($result, true);
  90. }
  91. //图片审核
  92. public function imgVerify($img)
  93. {
  94. $redis = new Redis();
  95. $token = $redis->get("filterToken");
  96. if (empty($token)) {
  97. $token = $this->getToken();
  98. }
  99. $curl = $this->imgUrl . "?access_token=" . $token;
  100. $bodys = array(
  101. 'image' => $img,
  102. 'scenes' => array("ocr",
  103. "face", "public", "politician", "antiporn", "terror", "webimage", "disgust",
  104. 'watermark')
  105. );
  106. $bodys = json_encode($bodys);
  107. $result = self::request_post($curl, $bodys, "img");
  108. return json_decode($result, true);
  109. }
  110. //头像审核
  111. public function avatarVerify($img)
  112. {
  113. $redis = new Redis();
  114. $token = $redis->get("filterToken");
  115. if (empty($token)) {
  116. $token = $this->getToken();
  117. }
  118. $curl = $this->avatarUrl . "?access_token=" . $token;
  119. $bodys = array(
  120. "configId" => "1",
  121. "images" => $img
  122. );
  123. $result = self::request_post($curl, $bodys, "text");
  124. return json_decode($result, true);
  125. }
  126. }

推荐教程:PHP教程

以上就是利用PHP和百度ai实现文本以及图片的审核的详细内容,更多请关注Gxlcms其它相关文章!

人气教程排行