您好,欢迎访问一九零五行业门户网

贴一段微信公众平台发送模板消息的代码

贴一段微信公众平台发送模板消息的代码,网上有关这方面的资料比较少,本人亲测以后分享出来,供新手学习交流!
第一步:先构造一个向远程服务器提交数据的函数
//构造一个请求函数
function http_request($url,$data=array()){
$ch = curl_init();
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);
// 我们在post数据哦!
curl_setopt($ch, curlopt_post, 1);
// 把post的变量加上
curl_setopt($ch, curlopt_postfields, $data);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
第二步:正式开始
public function sendtpl_msg(){
$appid=wxa46e292d23a7104b;
$appsecret=24feffbfce0ad7b3868345c96cf6eae5;
$json_token=http_request(https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=.$appid.&secret=.$appsecret);
$access_token=json_decode($json_token,true);
//获得access_token
$this->access_token=$access_token[access_token];
//echo $this->access_token;exit;
//模板消息
$template=array(
'touser'=>$_get['wecha_id'],
'template_id'=>vkz0bntigsbxfujkckdgl_fvl9qsksqewecfikq1dy4,
'url'=>http://weixin.qq.com/download,
'topcolor'=>#7b68ee,
'data'=>array(
'first'=>array('value'=>urlencode(您好,您已购买成功),'color'=>#743a3a),
'name'=>array('value'=>urlencode(商品信息:微时代电影票),'color'=>'#eeeeee'),
'remark'=>array('value'=>urlencode('永久有效!密码为:1231313'),'color'=>'#ffffff'),
)
);
$json_template=json_encode($template);
//echo $json_template;
//echo $this->access_token;
$url=https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=.$this->access_token;
$res=http_request($url,urldecode($json_template));
if ($res[errcode]==0) echo '模板消息发送成功!';
//print_r($res);
}
ad:真正免费,域名+虚机+企业邮箱=0元
其它类似信息

推荐信息