php小程序模板,php小程序模板消息

/**

*小程序 发送模板消息

*/

public function send_template_news()

{

$form_id = ''; //form_id 有两种 一种为提交过表单后生成的form_id 一种为支付后生成的prepay_id

$template_id = ''; //小程序后台设置的模板id

$openid = '' ; //用户的openid

$access_token = $this->get_access_token(); //access_token

$url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=$access_token";

$data = [

//此处数据对应与小程序后台设置的模板结构

"keyword1" => ["value" => $order_no],//订单号

"keyword3" => ["value" => $payment_money],//支付金额

"keyword4" => ["value" => date('Y-m-d H:i:s',$create_time)],//支付时间

];

$post = [

"touser" => $openid,

"template_id" => $template_id,

"page" => "pages/index/index", //点击

"form_id" => $prepay_id,

"data" => $data,

];

$this->http($url, json_encode($post));

}

获取access_token

public function get_access_token()

{

if (cache('access_token')){ //此处为tp5cache缓存

$access_token = cache('access_token');

}else{

$WX_APPID = ''; //小程序appid

$WX_SECRET = ''; //小程序appsecret

$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$WX_APPID.'&secret='.$WX_SECRET;

$infos = json_decode(file_get_contents($url));

$access_token = $infos->access_token;

cache('access_token',$access_token,7000);

}

return $access_token;

}

发送http请求

/** Get 或 Post 请求

* @param $url

* @param null $data 为null时,为post方法

* @return mixed

*/

public function http($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;

}

标签:url,程序,access,token,curl,php,data,id,模板

来源: https://blog.csdn.net/qd55815634/article/details/90270302

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值