Template.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace App\Http\Controllers\Wechat;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use Ixudra\Curl\Facades\Curl;
  6. class Template extends Controller
  7. {
  8. /**
  9. * 模板消息
  10. */
  11. public static function send($touser, $template_id, $url, $array, $color = null)
  12. {
  13. $default_color = [
  14. 'first' => '#173177',
  15. 'remark' => '#FA8F98'
  16. ];
  17. $data = null;
  18. foreach ($array as $key => $value) {
  19. $data[$key] = [
  20. 'value' => $value,
  21. 'color' => @$color[$key] ?: @$default_color[$key],
  22. ];
  23. }
  24. $post = json_encode([
  25. 'touser' => $touser,
  26. 'template_id' => $template_id,
  27. 'url' => $url,
  28. 'data' => $data
  29. ]);
  30. $http = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . Base::getAccessToken();
  31. $result = json_decode(Curl::to($http, $post)->withData($post)->post(), true);
  32. if ($result['errcode'] == 0) {
  33. return ['code' => $result['errcode'], 'msg' => $result['errmsg'], 'data' => ['msgid' => $result['msgid']]];
  34. } else {
  35. return ['code' => $result['errcode'], 'msg' => $result['errmsg']];
  36. }
  37. }
  38. }