本文实例讲述了php实现发送微信模板消息的方法。分享给大家供大家参考。具体如下:
该方法基于thinkphp实现实现,具体OrderPush.class.php文件如下:
复制代码 代码如下:<"https://api.weixin.qq.com/cgi-bin/token" . $appid . "&secret=" . $appsecret;
$token = $this->request_get($url);
$token = json_decode(stripslashes($token));
$arr = json_decode(json_encode($token), true);
$access_token = $arr['access_token'];
S($appid, $access_token, 720);
}
return $access_token;
}
/**
* 发送自定义的模板消息
* @param $touser
* @param $template_id
* @param $url
* @param $data
* @param string $topcolor
* @return bool
*/
public function doSend($touser, $template_id, $url, $data, $topcolor = '#7B68EE')
{
/*
* data=>array(
'first'=>array('value'=>urlencode("您好,您已购买成功"),'color'=>"#743A3A"),
'name'=>array('value'=>urlencode("商品信息:微时代电影票"),'color'=>'#EEEEEE'),
'remark'=>array('value'=>urlencode('永久有效!密码为:1231313'),'color'=>'#FFFFFF'),
)
*/
$template = array(
'touser' => $touser,
'template_id' => $template_id,
'url' => $url,
'topcolor' => $topcolor,
'data' => $data
);
$json_template = json_encode($template);
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send" . $this->accessToken;
$dataRes = $this->request_post($url, urldecode($json_template));
if ($dataRes['errcode'] == 0) {
return true;
} else {
return false;
}
}
}
希望本文所述对大家的php程序设计有所帮助。