Template.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | WeChatDeveloper
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2023 ThinkAdmin [ thinkadmin.top ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // | 免责声明 ( https://thinkadmin.top/disclaimer )
  11. // +----------------------------------------------------------------------
  12. // | gitee 代码仓库:https://gitee.com/zoujingli/WeChatDeveloper
  13. // | github 代码仓库:https://github.com/zoujingli/WeChatDeveloper
  14. // +----------------------------------------------------------------------
  15. namespace WeChat;
  16. use WeChat\Contracts\BasicWeChat;
  17. /**
  18. * 模板消息
  19. * Class Template
  20. * @package WeChat
  21. */
  22. class Template extends BasicWeChat
  23. {
  24. /**
  25. * 设置所属行业
  26. * @param string $industry_id1 公众号模板消息所属行业编号
  27. * @param string $industry_id2 公众号模板消息所属行业编号
  28. * @return array
  29. * @throws Exceptions\InvalidResponseException
  30. * @throws Exceptions\LocalCacheException
  31. */
  32. public function setIndustry($industry_id1, $industry_id2)
  33. {
  34. $url = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=ACCESS_TOKEN";
  35. $this->registerApi($url, __FUNCTION__, func_get_args());
  36. return $this->httpPostForJson($url, ['industry_id1' => $industry_id1, 'industry_id2' => $industry_id2]);
  37. }
  38. /**
  39. * 获取设置的行业信息
  40. * @return array
  41. * @throws Exceptions\InvalidResponseException
  42. * @throws Exceptions\LocalCacheException
  43. */
  44. public function getIndustry()
  45. {
  46. $url = "https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token=ACCESS_TOKEN";
  47. $this->registerApi($url, __FUNCTION__, func_get_args());
  48. return $this->httpGetForJson($url);
  49. }
  50. /**
  51. * 获得模板ID
  52. * @param string $tpl_id 板库中模板的编号,有“TM**”和“OPENTMTM**”等形式
  53. * @return array
  54. * @throws Exceptions\InvalidResponseException
  55. * @throws Exceptions\LocalCacheException
  56. */
  57. public function addTemplate($tpl_id)
  58. {
  59. $url = "https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=ACCESS_TOKEN";
  60. $this->registerApi($url, __FUNCTION__, func_get_args());
  61. return $this->httpPostForJson($url, ['template_id_short' => $tpl_id]);
  62. }
  63. /**
  64. * 获取模板列表
  65. * @return array
  66. * @throws Exceptions\InvalidResponseException
  67. * @throws Exceptions\LocalCacheException
  68. */
  69. public function getAllPrivateTemplate()
  70. {
  71. $url = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=ACCESS_TOKEN";
  72. $this->registerApi($url, __FUNCTION__, func_get_args());
  73. return $this->httpGetForJson($url);
  74. }
  75. /**
  76. * 删除模板ID
  77. * @param string $tpl_id 公众帐号下模板消息ID
  78. * @return array
  79. * @throws Exceptions\InvalidResponseException
  80. * @throws Exceptions\LocalCacheException
  81. */
  82. public function delPrivateTemplate($tpl_id)
  83. {
  84. $url = "https://api.weixin.qq.com/cgi-bin/template/del_private_template?access_token=ACCESS_TOKEN";
  85. $this->registerApi($url, __FUNCTION__, func_get_args());
  86. return $this->httpPostForJson($url, ['template_id' => $tpl_id]);
  87. }
  88. /**
  89. * 发送模板消息
  90. * @param array $data
  91. * @return array
  92. * @throws Exceptions\InvalidResponseException
  93. * @throws Exceptions\LocalCacheException
  94. */
  95. public function send(array $data)
  96. {
  97. $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
  98. $this->registerApi($url, __FUNCTION__, func_get_args());
  99. return $this->httpPostForJson($url, $data);
  100. }
  101. }