MiniFileLimitService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: weifeng
  5. * Date: 2020-05-22
  6. * Time: 18:27
  7. *
  8. * .--, .--,
  9. * ( ( \.---./ ) )
  10. * '.__/o o\__.'
  11. * {= ^ =}
  12. * > - <
  13. * / \
  14. * // \\
  15. * //| . |\\
  16. * "'\ /'"_.-~^`'-.
  17. * \ _ /--' `
  18. * ___)( )(___
  19. * (((__) (__))) 梦之所想,心之所向.
  20. */
  21. namespace app\common\services;
  22. use app\common\facades\Setting;
  23. class MiniFileLimitService
  24. {
  25. public function checkImg($file)
  26. {
  27. $url = $this->getImgSecCheckUrl();
  28. $obj = new \CURLFile($file->path(), $file->getMimeType(), $file->getClientOriginalName());
  29. $data = [
  30. 'media' => $obj,
  31. ];
  32. $result = $this->curl_post($url, $data, $options = array());
  33. return json_decode($result, JSON_FORCE_OBJECT);
  34. }
  35. public function checkMedia($media_url, $type)
  36. {
  37. $url = $this->getMediaCheckAsyncUrl();
  38. $data = [
  39. 'media_url' => $media_url,
  40. 'media_type' => $type,
  41. ];
  42. $result = $this->curl_post($url, $data, $options = array());
  43. return json_decode($result, JSON_FORCE_OBJECT);
  44. }
  45. public function checkMsg($content)
  46. {
  47. $url = $this->getMsgSecCheckUrl();
  48. $data = '{ "content":" ' . $content . ' " }';
  49. $result = $this->curl_post($url, $data, $options = array());
  50. return json_decode($result, JSON_FORCE_OBJECT);
  51. }
  52. public function getToken()
  53. {
  54. $tokenUrl = $this->getTokenUrl();
  55. $res = self::curl_post($tokenUrl, $post_data = '', $options = array());
  56. $data = json_decode($res, JSON_FORCE_OBJECT);
  57. return $data['access_token'];
  58. }
  59. private function curl_post($url = '',$post_data = '',$options = array())
  60. {
  61. $ch = curl_init($url);
  62. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  63. curl_setopt($ch,CURLOPT_POST,1);
  64. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  65. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  66. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  67. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  68. if(!empty($options)){
  69. curl_setopt_array($ch, $options);
  70. }
  71. $data = curl_exec($ch);
  72. curl_close($ch);
  73. return $data;
  74. }
  75. private function getImgSecCheckUrl()
  76. {
  77. return "https://api.weixin.qq.com/wxa/img_sec_check?access_token=" . $this->getToken();
  78. }
  79. private function getMediaCheckAsyncUrl()
  80. {
  81. return "https://api.weixin.qq.com/wxa/media_check_async?access_token=" . $this->getToken();
  82. }
  83. private function getMsgSecCheckUrl()
  84. {
  85. return "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=" . $this->getToken();
  86. }
  87. private function getTokenUrl()
  88. {
  89. $set = Setting::get('plugin.min_app');
  90. // $set['key'] = 'wxbe88683bd339aaf5';
  91. // $set['secret'] = 'fcf189d2a18002a463e7b675cea86c87';
  92. return "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $set['key'] . "&secret=" . $set['secret'];
  93. }
  94. }