對于公衆号内模闆消息功能的使用,首先去微信公衆平台開通模闆消息功能,認證的時間大約是3天。認證通過後選擇模闆,獲得消息模闆ID。此功能主要用于微信公衆号消息提醒,比如客戶的商家認證稽核通過,使用者購買商品成功等功能提醒,可以有效減少網站項目開發所必要的資金節約。下面重點來講解一下接口代碼:
接口代碼如下:
// 字元編碼
header("Content-Type:text/html; charset=utf-8");
// 微信接口類
class Wchat{
private static $appid;
private static $appsecret;
function __construct(){
self::$appid = '';
self::$appsecret ='';
}
// 微信授權位址
public static function getAuthorizeUrl($url){
$url_link = urlencode($url);
return "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . self::$appid . "&redirect_uri={$url_link}&response_type=code&scope=snsapi_base&state=1#Wchat_redirect";
}
// 擷取TOKEN
public static function getToken(){
$urla = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . self::$appid . "&secret=" . self::$appsecret;
$outputa = self::curlGet($urla);
$result = json_decode($outputa, true);
return $result['access_token'];
}
public static function getUserInfo($code, $weiwei_token){
$access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . self::$appid . "&secret=" . self::$appsecret . "&code={$code}&grant_type=authorization_code";
$access_token_json = self::curlGet($access_token_url);
$access_token_array = json_decode($access_token_json, true);
$openid = $access_token_array['openid'];
$new_access_token = $weiwei_token;
//全局access token獲得使用者基本資訊
$userinfo_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$new_access_token}&openid={$openid}";
$userinfo_json = self::curlGet($userinfo_url);
$userinfo_array = json_decode($userinfo_json, true);
return $userinfo_array;
}
public static function pushMessage($data = [],$topcolor = '#0000'){
$template = [
'touser' => $data['openid'],
'template_id' => $data['template_id'],
'url' => $data['url'],
'topcolor' => $topcolor,
'data' => $data['data']
];
$json_template = json_encode($template);
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . self::getToken();
$result = self::curlPost($url, urldecode($json_template));
$resultData = json_decode($result, true);
return $resultData;
}
public static function addLog($log_content = ''){
$data = "";
$data .= "DATE: [ " . date('Y-m-d H:i:s') . " ]\r\n";
$data .= "INFO: " . $log_content . "\r\n\r\n";
file_put_contents('/Wchat.log', $data, FILE_APPEND);
}
private static function curlGet($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
if(curl_errno($curl)){
return 'ERROR ' . curl_error($curl);
}
curl_close($curl);
return $output;
}
private static function curlPost($url, $data = null){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
if(!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
}
function get_page_url($type = 0){
$pageURL = 'http';
if($_SERVER["HTTPS"] == 'on'){
$pageURL .= 's';
}
$pageURL .= '://';
if($type == 0){
$pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
}else{
$pageURL .= $_SERVER["SERVER_NAME"];
}
return $pageURL;
}
// 擷取使用者openid
$Wchat = new Wchat();
if(empty($_GET['code']) || !isset($_GET['code'])){
// 通過授權擷取code
$url = get_page_url();
$authorize_url = $Wchat->getAuthorizeUrl($url);
header("Location:{$authorize_url}"); // 重定向浏覽器
exit();
}else{
// 擷取微信使用者資訊
$uid=session("uid");
$info=M("user")->where("uid=$uid")->find();
$openid=$info['openid'];
$list=M("good")->where("uid=$uid and fabu=2")->select();
foreach ($list as $key => $value) {
$good_id[]=$value['id'];
}
$where['shop_uid']=$uid;
$where['good_id']=array("in",$good_id);
$where['price_state']=1;
$res=M("good_pai_price")->where($where)->order("addtime desc")->limit(1)->find();
$id=$res['good_id'];
$mai_uid=$res['uid'];
$goodinfo=M("good")->where("id=$id")->find();
// echo $mai_uid;exit;
$userinfo=M("user")->where("uid=$mai_uid")->find();
// var_dump($goodinfo);exit;
$code = $_GET['code'];
$weiwei_token = $Wchat->getToken(); // 擷取微信token
$user_info = $Wchat->getUserInfo($code, $weiwei_token);
// $openid = $user_info['openid'];
# 公衆号消息推送
$Wchat::pushMessage([
'openid' => $openid, // 使用者openid
'access_token' => $weiwei_token,
'template_id' => "", // 填寫你自己的消息模闆ID
'data' => [ // 模闆消息内容,根據模闆詳情進行設定
'first' => ['value' => urlencode("尊敬的".$info['nickname'].",您好,您的拍品已有人出價。"),'color' => "#743A3A"],
'keyword1' => ['value' => urlencode($goodinfo['desc']),'color'=>'blue'],
'keyword2' => ['value' => urlencode(date("Y-m-d H:i",$res['addtime'])),'color'=>'blue'],
'keyword3' => ['value' => urlencode($userinfo['name']),'color' => 'green'],
'keyword4' => ['value' => urlencode($userinfo['s_province']),'color' => 'green'],
'keyword5' => ['value' => urlencode($res['price']/100),'color' => 'green'],
'remark' => ['value' => urlencode("更多出價詳情,請點選頁面進行實時查詢。"),'color' => '#743A3A']
],
'url' => 'http://www.xxx.com/Shop/info?id='.$id // 消息跳轉連結
]);
}
2. php調用接口代碼
Php代碼如下:
public function msg(){
require_once 'WchatAction.class.php';
// 微信接口類
$wchat = new \Wchat();
$wchat->pushMessage();
}
好了,以上就是全部的源碼了,如果大家還是存在不了解的地方,可以留言咨詢。
本文由專業的鄭州小程式開發公司燚軒科技整理釋出,原創不易,如需轉載請注明出處!