微信订阅号开发笔记(三)
时间:2022-04-06 10:59
2、发送客服消息
当用户主动发消息给公众号的时候(包括发送信息、点击自定义菜单、订阅事件、扫描二维码事件、支付成功事件、用户维权),微信将会把消息数据推送给开发者,开发者在一段时间内(目前修改为48小时)可以调用客服消息接口,通过POST一个JSON数据包来发送消息给普通用户,在48小时内不限制发送次数。此接口主要用于客服等有人工消息处理环节的功能,方便开发者为用户提供更加优质的服务。
获取u
//发送客服消息 public function sendMessage(){ $url="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="; $url.=$this->getacctoken(); //可发送 文本 图片 语音 视频 音乐 图文 模版详见http://mp.weixin.qq.com/wiki/index.php?title=%E5%8F%91%E9%80%81%E5%AE%A2%E6%9C%8D%E6%B6%88%E6%81%AF $post_data=‘ { "touser":"a openId ", "msgtype":"text", "text": { "content":"Hello World" } } ‘; $ret = $this->cpost($url,$post_data); print_r( $ret ); }
3、基础 获取access_token
//获取access_token //查找数据库 发现过期 重新获取 存入数据库 // 有效期内 返回存储的access_token public function getacctoken(){ $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".APPSECRET; $accesstoken = M("accesstoken"); // 实例化wlog对象 $atoken = $accesstoken ->where(‘aid=1 ‘)->find(); $time = time(); if($atoken){ $expires_in=$accesstoken ->where(‘aid=1‘)->getField(‘expires_in‘); if($time>=$expires_in){ $adata = $this->cget($url); echo "time is = ".$time."access_token = ".$adata[‘access_token‘]; $data[‘access_token‘]= $adata[‘access_token‘]; $data[‘expires_in‘]=$time + $adata[‘expires_in‘]; $accesstoken->where(‘aid=1‘)->save($data); return $adata[‘access_token‘]; }else{ return $accesstoken ->where(‘aid=1‘)->getField(‘access_token‘); } }else{ $adata = $this->cget($url); echo "time is = ".$time."access_token = ".$adata[‘access_token‘]; $data[‘access_token‘]= $adata[‘access_token‘]; $data[‘expires_in‘]=$time + $adata[‘expires_in‘]; $accesstoken->where(‘aid=1‘)->save($data); return $adata[‘access_token‘]; } }
本文出自 “” 博客,请务必保留此出处
微信订阅号开发笔记(三),布布扣,bubuko.com