小程序订阅消息【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版】的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-28怎么把PHP程序打包?-icode9专业技术文章分享
- 2024-09-28怎么用Phar打包PHP程序?-icode9专业技术文章分享
- 2024-09-13手动在github上下载的mfpt包,怎么放到thinkphp5.0框架并正常使用-icode9专业技术文章分享
- 2024-09-05python的<class 'bytearray'>相当于php的哪个数据类型-icode9专业技术文章分享
- 2024-09-05php 导出银行卡号避免科学技术法的方法-icode9专业技术文章分享
- 2024-08-30什么样的php代码质量差被称为垃圾代码-icode9专业技术文章分享
- 2024-08-30用 PHP 调用拼多多的接口以获取订单状态消息的步骤方法和代码示例-icode9专业技术文章分享
- 2024-08-27phpunit单元测试框架的入门和使用方法介绍-icode9专业技术文章分享
- 2024-08-24PHP 中date("w") 周一是多少-icode9专业技术文章分享
- 2024-08-14thinkphp8.0获取域名或主机名方法-icode9专业技术文章分享