secCheckApi.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * secCheckApi.php UTF-8
  4. * 铭感检测接口
  5. *
  6. * @date : 2018/12/3 13:50
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : HuoMp 1.0
  11. */
  12. namespace huomp\controller\sec;
  13. use huo\controller\common\Base;
  14. use huolib\status\CommonStatus;
  15. use huoMpMsg\controller\Common;
  16. class secCheckApi extends Base {
  17. /**
  18. * 检查一段文本是否含有违法违规内容
  19. * https://developers.weixin.qq.com/minigame/dev/api/msgSecCheck.html
  20. *
  21. * @param string $wx_app_id 第三方用户唯一凭证
  22. * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
  23. * @param string $content 检测文本内容
  24. *
  25. * @return array
  26. */
  27. public function msgSecCheck($wx_app_id, $wx_app_secret, $content) {
  28. $_url = 'https://api.weixin.qq.com/wxa/msg_sec_check';
  29. $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
  30. $_url = $_url.'?access_token='.$_access_token;
  31. $_param = ['content' => $content];
  32. $_param_json = json_encode($_param, JSON_UNESCAPED_UNICODE);
  33. $_header = ['Content-Type: application/octet-stream; charset=utf-8'];
  34. $_ret = Common::curl($_url, $_param_json, 'POST', $_header);
  35. $_rdata = [];
  36. if (Common::isJson($_ret)) {
  37. $_rdata = json_decode($_ret, true);
  38. }
  39. if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) {
  40. $_code = $_rdata['errcode'];
  41. return $this->huoError($_code, $_rdata['errmsg']);
  42. }
  43. $_code = CommonStatus::NO_ERROR;
  44. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  45. }
  46. /**
  47. * 校验一张图片是否含有违法违规内容。
  48. * https://developers.weixin.qq.com/minigame/dev/api/imgSecCheck.html
  49. *
  50. * @param string $wx_app_id 第三方用户唯一凭证
  51. * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
  52. * @param string $file 要检测的图片文件
  53. *
  54. * @return array
  55. */
  56. public function imgSecCheck($wx_app_id, $wx_app_secret, $file) {
  57. $_url = 'https://api.weixin.qq.com/wxa/img_sec_check';
  58. $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
  59. $_url = $_url.'?access_token='.$_access_token;
  60. $cfile = curl_file_create($file);
  61. $_img_data = array('media' => $cfile);
  62. $curl = curl_init();
  63. curl_setopt($curl, CURLOPT_URL, $_url);
  64. curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
  65. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
  66. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
  67. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  68. curl_setopt($curl, CURLOPT_POST, 1);
  69. curl_setopt($curl, CURLOPT_POSTFIELDS, $_img_data);
  70. /* https 请求 */
  71. if (strlen($_url) > 5 && strtolower(substr($_url, 0, 5)) == "https") {
  72. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  73. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  74. }
  75. $_ret = curl_exec($curl);
  76. if (false === $_ret) {
  77. $_ret = curl_errno($curl);
  78. }
  79. curl_close($curl);
  80. if (Common::isJson($_ret)) {
  81. $_rdata = json_decode($_ret, true);
  82. }
  83. if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) {
  84. $_code = $_rdata['errcode'];
  85. return $this->huoError($_code, $_rdata['errmsg']);
  86. }
  87. $_code = CommonStatus::NO_ERROR;
  88. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  89. }
  90. }