Template.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 $templateIdShort 板库中模板的编号,有“TM**”和“OPENTMTM**”等形式
  53. * @param array $keywordNameList 选用的类目模板的关键词
  54. * @return array
  55. * @throws Exceptions\InvalidResponseException
  56. * @throws Exceptions\LocalCacheException
  57. */
  58. public function addTemplate($templateIdShort, $keywordNameList = [])
  59. {
  60. $url = "https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=ACCESS_TOKEN";
  61. $this->registerApi($url, __FUNCTION__, func_get_args());
  62. return $this->httpPostForJson($url, ['template_id_short' => $templateIdShort, 'keyword_name_list' => $keywordNameList]);
  63. }
  64. /**
  65. * 获取模板列表
  66. * @return array
  67. * @throws Exceptions\InvalidResponseException
  68. * @throws Exceptions\LocalCacheException
  69. */
  70. public function getAllPrivateTemplate()
  71. {
  72. $url = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=ACCESS_TOKEN";
  73. $this->registerApi($url, __FUNCTION__, func_get_args());
  74. return $this->httpGetForJson($url);
  75. }
  76. /**
  77. * 删除模板ID
  78. * @param string $tpl_id 公众帐号下模板消息ID
  79. * @return array
  80. * @throws Exceptions\InvalidResponseException
  81. * @throws Exceptions\LocalCacheException
  82. */
  83. public function delPrivateTemplate($tpl_id)
  84. {
  85. $url = "https://api.weixin.qq.com/cgi-bin/template/del_private_template?access_token=ACCESS_TOKEN";
  86. $this->registerApi($url, __FUNCTION__, func_get_args());
  87. return $this->httpPostForJson($url, ['template_id' => $tpl_id]);
  88. }
  89. /**
  90. * 发送模板消息
  91. * @param array $data
  92. * @return array
  93. * @throws Exceptions\InvalidResponseException
  94. * @throws Exceptions\LocalCacheException
  95. */
  96. public function send(array $data)
  97. {
  98. $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
  99. $this->registerApi($url, __FUNCTION__, func_get_args());
  100. return $this->httpPostForJson($url, $data);
  101. }
  102. }