小程序订阅消息【PHP版】

2021/7/15 17:06:54

本文主要是介绍小程序订阅消息【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 = "o0Lcm0dq6wBY6QQCQt6R-uAFciCc";

//模板id

$template_id = "f59Sdp0sK2Ww1pbTd7LcvTsjJWA197dnx21TfYXSGog";

$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;

}

function posturl($url,$data){ /** curl post请求封装,返回json数据格式数据 **/

  $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);

}



这篇关于小程序订阅消息【PHP版】的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程