公眾號模板消息功能的開發與設置
接口代碼如下:
// 字符編碼
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‘];
}
/**
-
getUserInfo 獲取用戶信息
-
@param string $code 微信授權code
-
@param string $weiwei_token Token
- @return array
*/
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;
}
/**
-
pushMessage 發送自定義的模板消息
- @param array $data 模板數據
$data = [
‘openid‘ => ‘‘, 用戶openid
‘url‘ => ‘‘, 跳轉鏈接
‘template_id‘ => ‘‘, 模板id
‘data‘ => [ // 消息模板數據
‘first‘ => [‘value‘ => urlencode(‘黃旭輝‘),‘color‘ => "#743A3A"],
‘keyword1‘ => [‘value‘ => urlencode(‘男‘),‘color‘=>‘blue‘],
‘keyword2‘ => [‘value‘ => urlencode(‘1993-10-23‘),‘color‘ => ‘blue‘],
‘remark‘ => [‘value‘ => urlencode(‘我的模板‘),‘color‘ => ‘#743A3A‘]
]
];
-
@param string $topcolor 模板內容字體顏色,不填默認為黑色
- @return 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;
}
/**
-
addLog 日誌記錄
- @param string $log_content 日誌內容
*/
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);
}
/**
-
發送get請求
-
@param string $url 鏈接
- @return bool|mixed
*/
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;
}
/**
-
發送post請求
-
@param string $url 鏈接
-
@param string $data 數據
- @return bool|mixed
*/
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;
}
}
/**
-
get_page_url 獲取完整URL
- @return url
*/
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.xxxx.com/Shop/info?id=‘.$id // 消息跳轉鏈接
]);
}
- php調用接口代碼
Php代碼如下:
public function msg(){
require_once ‘WchatAction.class.php‘;
// 微信接口類
$wchat = new \Wchat();
$wchat->pushMessage();
}
好了,以上就是全部的源碼了,如果大家還是存在不理解的地方,可以留言咨詢。
本文由專業的鄭州小程序開發公司燚軒科技整理發布,原創不易,如需轉載請註明出處!
公眾號模板消息功能的開發與設置