小程序订阅消息【PHP版】
效果图

public function actionSubmessage($User,$Tel,$Type) {
//配置appid
$appid = "wx81****************b9";
//配置appscret
$secret = "c2a7***********************bde";
//校验api:请勿改动
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
//获取access_token
$access_token = $this -> geturl($url)["access_token"];
//固定api:请勿改动
$url_2 = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=".$access_token;
//要推送到的用户的openid
$openid = "o0L*************************Cc";
//模板id
$template_id = "f59Sdp0s***********************************XSGog";
$data = [];
//接收者(用户)的 openid
$data['touser'] = $openid;
//所需下发的订阅模板id
$data['template_id'] = $template_id;
//点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
$data['page'] = "pages/index/index";
//模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any } }
$data['data'] = [
"name1" => [ 'value' => $User ],
"thing2" => [ 'value' => $Tel ],
"date3" => [ 'value' => date('Y-m-d H:i', time()) ],
"phrase4" => [ 'value' => $Type ],
"thing5" => [ 'value' => '有新的用户已报名,请尽快联系' ]
];
//跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
//$data['miniprogram_state'] = 'developer';
$this->success("调用消息",$this -> posturl($url_2, $data));
//var_dump($this -> posturl($url_2, $data));
}
function geturl($url){ /** curl get请求封装,返回json数据格式数据 **/ $headerArray =array("Content-type:application/json;","Accept:application/json"); $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); curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray); $output = curl_exec($ch); curl_close($ch); $output = json_decode($output,true);
return $output; }
/** curl post请求封装,返回json数据格式数据 **/
function posturl($url,$data){
$data = json_encode($data); $headerArray =array("Content-type:application/json;charset='utf-8'","Accept:application/json"); $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_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); return json_decode($output,true); }

浙公网安备 33010602011771号