Blogger Information
Blog 17
fans 0
comment 1
visits 21517
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP 发送小程序订阅消息
Ant.
Original
2156 people have browsed it

发送订阅消息之前你先去申请模板 , 公共的直接选用就可以, 没有符合的你还得申请,审核时间有点久,
然后根据你申请的模板来填写模板需要的参数就好了, 前提是用户要授权接收订阅消息 , 不然用户是收不到的 , 如果想要以后可以实现自动发送, 可以引导用户永久授权 . 这样就不用每次都需要授权,因为现在订阅消息只有一次性订阅消息

  1. //根据小程序里面的模板消息来设置
  2. $msg = [
  3. 'time3' => ['value'=> date('Y-m-d H:i:s', time())],
  4. 'thing4' => ['value'=> $userName['name']],
  5. 'thing2' => ['value'=> $userInfo['content']],
  6. 'phrase8' => ['value'=> "报价消息"],
  7. 'thing7' => ['value'=> "您当前有{$count}条未读消息, 可能需要尽快处理"],
  8. ];
  9. 用户的openid
  10. $sendSubscribeMessage[] = $this->sendSubscribeMessage($userOpneid['wx_openid'],
  11. 'Mpfk11CE1uTnElfGbsSQ9Y6wjejdXtg2fhSpxvQYSRw', 'pages/myPrice/index', $msg);
  12. //access_token
  13. $access_token = $this->getAccessToken();
  14. //请求url
  15. $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' . $access_token ;
  16. /***
  17. * @param $touser //需要接受订阅消息的openid
  18. * @param $template_id //模板ID
  19. * @param $page //点击返回页面路径
  20. * @param $dataArray //订阅格式请参照小程序订阅消息模板中的格式
  21. * @return array
  22. * 订阅消息模板
  23. */
  24. public function sendSubscribeMessage($touser, $template_id, $page, $dataArray)
  25. {
  26. //access_token 一般都存在缓存 ,
  27. $access_token = $this->getAccessToken();
  28. //请求url
  29. $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' . $access_token ;
  30. //发送内容
  31. $data = [] ;
  32. $data['touser'] = $touser; //用户的openid
  33. $data['template_id'] = $template_id; //所需下发的订阅模板id
  34. //点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
  35. $data['page'] = $page;
  36. //模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any } }
  37. $data['data'] = $dataArray;
  38. //跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
  39. $data['miniprogram_state'] = 'trial';
  40. $data['lang'] = 'zh_CN';
  41. $json_data = json_encode($data);
  42. $http = new Http();
  43. return [
  44. 'openid' => $touser,
  45. 'data' => $http->curlRequest($url, $json_data)//这里面就是个curl请求 , 转成数组返回
  46. ];
  47. }
  48. /***
  49. * @return mixed
  50. * 获取access_token
  51. */
  52. protected function getAccessToken()
  53. {
  54. $AccessToken = Cache::get('accessToken');
  55. if ($AccessToken) return $AccessToken; else{
  56. $model = new Http();
  57. $access_token = $model->curlRequest('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appid.'&secret='.$this->appsecret);
  58. $access_token = $access_token['access_token'];
  59. Cache::set('accessToken',$access_token, 5400);
  60. return Cache::get('accessToken');
  61. }
  62. }
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post