//小程序发送模板消息必须要求预支付id,预支付id是微信支付接口调起后返回 prepayId(根据实际要求需要处理掉部分参数)的; 或者formid; formid必须通过小程序表单提交 一个prepayId 可以发3次消息,一个formid 有效期只有七天,并且只能使用一次,使用后即失效
<?php
namespace app\wxpay\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;
class Sendinfo extends Controller{
public function send_msg()
{
$prepayId = $_GET['prepayId'];//我这里使用支付id
$temid ='S5hvfbTfCQXmLzqceoFPMfzYj0D7tKiDtrgJj5_fI6Y';//微信小程序的模板id
$page = 'pages/index/index';//用户点击消息进入小程序的页面
$openid = $_GET['openid'];//接收消息用户的openid
$access_token=$this->getAccessToken();
$url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.$access_token;
$data = array(
"touser"=>$openid,
"template_id"=>$temid,
"page"=>$page,
"form_id"=>$prepayId,
"data"=>array(
"keyword1"=>array(
"value"=>'自定义',
"color"=>"#173177"
),
"keyword2"=>array(
"value"=>'自定义',
"color"=>"#173177"
),
"keyword3"=>array(
"value"=>'自定义',
"color"=>"#173177"
),
"keyword4"=>array(
"value"=>'自定义',
"color"=>"#173177"
),
"keyword5"=>array(
"value"=>'自定义',
"color"=>"#173177"
),
"keyword6"=>array(
"value"=>'自定义',
"color"=>"#173177"
),
"keyword7"=>array(
"value"=>'自定义',
"color"=>"#173177"
),
"keyword8"=>array(
"value"=>'自定义',
"color"=>"#173177"
),
"keyword9"=>array(
"value"=>'自定义',
"color"=>"#173177"
)
),
// "emphasis_keyword"=>"keyword1.DATA",//需要进行加大的消息
);
$result = $this->https_curl_json($url,$data,'json');
if($result){
echo json_encode(array('state'=>4,'msg'=>$result));
}else{
echo json_encode(array('state'=>5,'msg'=>$result));
}
}
function https_curl_json($url,$data,$type){
if($type=='json'){//json $_POST=json_decode(file_get_contents('php://input'), TRUE);
$headers = array("Content-type: application/json;charset=UTF-8","Accept: application/json","Cache-Control: no-cache", "Pragma: no-cache");
$data=json_encode($data);
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
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);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers );
$output = curl_exec($curl);
if (curl_errno($curl)) {
echo 'Errno'.curl_error($curl);//捕抓异常
}
curl_close($curl);
return $output;
}
public function index()
{
$info = file_get_contents('php://input');
file_put_contents(APP_PATH.'../a.json',$info);
echo 'success';
}
public function getAccessToken()
{
$isExpires = $this->isExpires();
if($isExpires === false){
$appid='你的appid';
$appsecret='你的secret';
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret;
$res = $this->curl($url);
// dump($res);
$arr = json_decode($res,true);
if($arr && !isset($arr['errcode'])){
$arr['time'] = time();
file_put_contents(APP_PATH . '../payaccess_token.json', json_encode($arr));//将得到的access_token存到服务器。目录可以自己设置,我的是存到根目录
return $arr['access_token'];
}else{
echo 'error on get access_token';die;
}
}else{
return $isExpires;
}
}
public function isExpires(){//判断存在服务器的access_token是否过期
if(!file_exists(APP_PATH . '../payaccess_token.json')){
return false;
}
$res = file_get_contents(APP_PATH . '../payaccess_token.json');
$arr = json_decode($res,true);
if($arr && time()<(intval($arr['time'])+intval($arr['expires_in']))){
//未过期
return $arr['access_token'];
}else{
return false;
}
}
public function curl($url)
{ //初始化
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
// 执行后不直接打印出来
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
// 跳过证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// 不从证书中检查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//执行并获取HTML文档内容
$output = curl_exec($ch);
//释放curl句柄
curl_close($ch);
return $output;
}
}
最后得到的结果