您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
微信公众平台接口开发-模板消息
发布时间:2016-11-13 14:34:07编辑:雪饮阅读()
最近项目上要求做一个私信功能。而我看官方文档中并没有关于公众平台下,粉丝之间进行通信的说明。
但是可以灵活利用模板消息,以公众平台做为中间人来传递消息。粉丝间的通信就通过传入不同的粉丝的openid即可。
如下是我写的一个类。
<?php
class Privateletter{
public $appid="wxef9a281b18780ed1";
public $access="0af800fdb331de30c7dfe8503449bb9e";
public $templetid="BpLTy30y-pMIF8wUKBXhSz3keu0TnGmU8MOCFOjiNKI";
public $touser="o4jSdwstI7ez68BbqL-LqrYqdz4A";
public function __construct($appid,$access,$templetid,$touser){
$this->appid=$appid;
$this->access=$access;
$this->templetid=$templetid;
$this->touser=$touser;
}
public function http_curl($url,$type='get',$res='json',$data=''){
//1.创建连接资源
$ch = curl_init();
//2.设置参数
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if($type=='post'){
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
}
//3.采集
$output = curl_exec($ch);
//4.关闭
curl_close($ch);
if($res='json'){
if(curl_errno($ch)){
return curl_errno($ch);
}
return json_decode($output,true);
}
}
public function access_token($appid,$access){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$access;
$res=$this->http_curl($url);
return $res['access_token'];
}
public function sendTemplateMsg(){
$access_token=$this->access_token($this->appid,$this->access);
$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
$array=array(
'touser'=>$this->touser,
'template_id'=>$this->templetid,
'url'=>'http://www.baidu.com',
'data'=>array(
'first'=>array('value'=>'hello','color'=>'#173177'),
'issueInfo'=>array('value'=>100,'color'=>'#173177'),
'betTime'=>array('value'=>date('Y-m-d H:i:s'),'color'=>'#173177'),
'fee'=>array('value'=>120,'color'=>'#173177'),
'drawTime'=>array('value'=>'雪饮','color'=>'#173177'),
'remark'=>array('value'=>'消息尾','color'=>'#173177')
),
);
$postJson=json_encode($array);
$res=$this->http_curl($url,'post','json',$postJson);
}
}
$appid="wxef9a281b18780ed1";
$access="0af800fdb331de30c7dfe8503449bb9e";
$templetid="BpLTy30y-pMIF8wUKBXhSz3keu0TnGmU8MOCFOjiNKI";
$touser="o4jSdwu_NgIjfqXcR1VpCD9nbOXI";
$a=new Privateletter($appid,$access,$templetid,$touser);
$a->sendTemplateMsg();
?>
关键字词:微信公众平台,模板消息