noticeUtil.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace common\components;
  3. //通知系统
  4. class noticeUtil
  5. {
  6. public static function push($message, $mobile_list = [])
  7. {
  8. if (getenv('YII_ENV') == 'production') {
  9. $url = "https://oapi.dingtalk.com/robot/send?access_token=0856d031e07890f1902496747a0576a49c3df51dcf1e16427168cce12892bedb";
  10. } else {
  11. $mobile_list = [];
  12. $url = 'https://oapi.dingtalk.com/robot/send?access_token=0809902bdb8fa5cfdddbbe36cf3d061558c87769a38c03168571a41913e0d238';
  13. }
  14. $data = [
  15. 'msgtype' => 'text',
  16. 'text' => ['content' => '【花卉宝】' . $message]
  17. ];
  18. if (!empty($mobile_list)) {
  19. $mobile_list = is_array($mobile_list) == false ? [(string)$mobile_list] : $mobile_list;
  20. $data['at']['atMobiles'] = $mobile_list;
  21. }
  22. $data_string = json_encode($data);
  23. self::request($url, $data_string);
  24. }
  25. public static function request($remote_server, $post_string)
  26. {
  27. $ch = curl_init();
  28. curl_setopt($ch, CURLOPT_URL, $remote_server);
  29. curl_setopt($ch, CURLOPT_POST, 1);
  30. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  31. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=utf-8'));
  32. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
  33. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  34. if (getenv('YII_ENV') == 'local') {
  35. // 线下环境不用开启curl证书验证, 未调通情况可尝试添加该代码
  36. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  37. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  38. }
  39. $data = curl_exec($ch);
  40. curl_close($ch);
  41. return $data;
  42. }
  43. }