<?php
/*
* 模板消息發送,電腦端測試時需要手動填寫openid
* 微信端會自動獲取當前openid發送無需填寫
*/
header("Content-type: text/html; charset=utf-8");
date_default_timezone_set("Asia/Shanghai");
$temp = new template();
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
if (!empty($_GET['code'])) {
$openid = $temp->GetOpenid();
if ($openid) {
$result = $temp->sendMsg($openid);
if ($result['errmsg'] == 'ok') {
echo '<script>alert("發送成功!");</script>';
} else {
echo '發送失敗!' . $result['errmsg'];
}
} else {
echo '<script>alert("獲取openID失敗!");</script>';
}
} else {
$temp->GET_Code();
}
} else {
echo '<script>alert("發送模板需要先獲取openID!");</script>';
$openid='orpdAwprTZvFaUwPDfW4WSanlZIk';
$result = $temp->sendMsg($openid);
if ($result['errmsg'] == 'ok') {
echo '<script>alert("發送成功!");</script>';
} else {
echo '發送失敗!' . $result['errmsg'];
}
}
class template {
public $appid = "xxxxxxxx";
public $secret = "xxxxxxxxxxxx";
public function sendMsg($openid) {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->appid . "&secret=" . $this->secret;
//獲取access_token
$resultData = $this->https_getRequest($url);
$access_token = $resultData["access_token"];
//$openid = 'orpdAwprTZvFaUwPDfW4WSanlZIk';
$templateid = 'hpTqa6GaczDqeIzuvfWqOiVHNm080yXH1yGnJKHC6jQ';
$title = "模板消息發送成功!";
$data1 = "測試模板消息發送";
$data2 = "開發員!";
$data3 = date("Y年m月d日 H:i", time());
$remark = "當前為測試模板可任意指定內容,但實際上正式帳號的模板消息,只能從模板庫中獲得";
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;
/*
* @miniprogram 跳小程序所需數據,不需跳小程序可不用傳該數據
* @appid appid必須與發模板消息的公眾號是綁定關聯關系)
* @pagepath 所需跳轉到小程序的具體頁面路徑,支持帶參數,(示例index?foo=bar)
*/
$data = '{
"touser":"' . $openid . '",
"template_id":"' . $templateid . '",
"url":"http://www.wxwytime.com/index.php?g=Wap&m=Index&a=search&token=lypzib1465694885",
"miniprogram":{
"appid":"",
"pagepath":""
},
"data":{
"first": {
"value":"' . $title . '",
"color":"#7A378B"
},
"keyword1":{
"value":"' . $data1 . '",
"color":"#787878"
},
"keyword2": {
"value":"' . $data2 . '",
"color":"#787878"
},
"keyword3": {
"value":"' . $data3 . '",
"color":"#787878"
},
"remark":{
"value":"' . $remark . '",
"color":"#C5C1AA"
}
}
}';
$send = $this->https_request($url, $data);
$send = json_decode($send, true);
return $send;
}
//發起獲得code值鏈接
public function GET_Code() {
$get_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $this->appid;
$url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header("location:" . $get_url . "&redirect_uri=" . $url . "&response_type=code&scope=snsapi_base&state=1#wechat_redirect");
}
//獲取用戶openid
public function GetOpenid() {
$get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $this->appid . '&secret=' . $this->secret . '&code=' . $_GET['code'] . '&grant_type=authorization_code';
$json_obj = $this->https_getRequest($get_token_url);
// $access_token = $json_obj['access_token'];
return $json_obj['openid'];
}
/*
* 獲取數據
*/
public function https_getRequest($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$jsonData = json_decode($output, true);
return $jsonData;
}
//通用數據處理方法發送模板消息
public function https_request($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;
}
}