wechattemplateapi.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * 模板消息
  4. * Created by 上海风车教育科技有限公司.
  5. * User: 刘红伟
  6. * Date: 15-12-10
  7. * Email: 454303753@qq.com
  8. * File:wechattemplateapi.php
  9. */
  10. class wechattemplateapi{
  11. private $url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";
  12. private $token;
  13. private $template_id;
  14. private $parameter=array();
  15. private $tem_type=1;
  16. public function __construct($token,$data=array(),$tem_type=1){
  17. $this->token=$token;
  18. $this->tem_type=$tem_type;
  19. $this->parameter=$data;
  20. }
  21. //选择模板
  22. private function create_tem(){
  23. switch($this->tem_type){
  24. case 1:
  25. $this->template_id='oSGXMc3RxDr92mPBYV80Z7yBp0LZ3ulMgY4sdaNn0Sk';
  26. $result= $this->test_template();
  27. break;
  28. }
  29. return $result;
  30. }
  31. //充值模板内容
  32. private function test_template(){
  33. //写模板内容
  34. return json_encode(array(
  35. 'touser'=>$this->parameter['openId'],
  36. 'template_id'=>$this->template_id,
  37. 'url'=> $this->parameter['url'],
  38. 'topcolor'=>'#000000',
  39. 'data'=>array(
  40. 'name'=>array('value'=>$this->parameter['name'],'color'=>'#000000')
  41. )
  42. )
  43. );
  44. }
  45. //发送模板消息
  46. public function send(){
  47. $this->url=$this->url.$this->token;
  48. $http = wx_http::factory($this->url, wx_http::TYPE_CURL);
  49. $json = $http->post($this->url, $this->create_tem(), array(
  50. 'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1)',
  51. 'Content-Type: application/json; charset=utf-8',
  52. 'Content-Length: '. strlen($this->create_tem())
  53. ));
  54. $result =json_decode($json,true);
  55. if(!empty($result) && $result['errcode']==0 and $result['errmsg']=='ok'){
  56. return true;
  57. }else{
  58. return false;
  59. }
  60. }
  61. }