Template.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace App\Services\WeChat;
  3. use Ixudra\Curl\Facades\Curl;
  4. class Template extends Base
  5. {
  6. /**
  7. * 获取模板列表
  8. * @return mixed
  9. * @throws WeChatException
  10. */
  11. public function getAll()
  12. {
  13. $access_token = $this->accessToken;
  14. $url = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token={$access_token}";
  15. $response = Curl::to($url)->asJson(true)->get();
  16. if (isset($response->errcode) && $response->errcode != 0) {
  17. throw new WeChatException($response->errmsg, $response->errcode);
  18. }
  19. return $response->template_list;
  20. }
  21. /**
  22. * 发送模板消息
  23. * @param array $data
  24. * @return mixed
  25. * @throws WeChatException
  26. */
  27. public function send($data)
  28. {
  29. $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
  30. $response = \Curl::to($url)->withData($data)->asJson(true)->get();
  31. if (isset($response['errcode']) && $response['errcode'] != 0) {
  32. throw new WeChatException($response['errmsg'], $response['errcode']);
  33. }
  34. return $response['msgid'];
  35. }
  36. }