微信公众号发送模板消息 -- PHP后台
生活随笔
收集整理的這篇文章主要介紹了
微信公众号发送模板消息 -- PHP后台
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
微信公眾號發送模板消息 – PHP后臺
注意:這里的用戶openid必須是和你的appid,appsecret是一整套的,所謂一整套的意思就是,openid是通過你當前的這個appid獲取到的,如果是通過其他appid獲取到的用戶openid,就會報錯openid無效。
直接看代碼吧:
/*** 發送模板消息*/public function send_notice(){$appid = '填寫你的appid';$appsecret = '填寫你的appsecret ';$access_token_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret;//獲取access_token$json_token=$this->curl_post($access_token_url);$access_token1=json_decode($json_token,true);$access_token2=$access_token1['access_token'];//模板消息$json_template = $this->json_tempalte();$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token2;$res = $this->curl_post($url,urldecode($json_template));if ($res['errcode']==0){return '發送成功';}else{return '發送失敗';}}/*** 將模板消息json格式化*/public function json_tempalte(){//模板消息$template=['touser' => '填寫用戶openid', //用戶openid'template_id' => "模板id", //在公眾號下配置的模板id'url' => "http://baidu.com", //點擊模板消息會跳轉的鏈接//如果想要跳轉微信小程序,就把上面這個url這一行注釋掉,用下面這個`miniprogram`// 'miniprogram' => [// 'appid' => '這里填寫要跳轉的小程序appid',// 'pagepath' => 'pages/index/index?order_id=205', //這里填寫小程序路徑,可以拼接參數// ],'topcolor' => "#7B68EE",'data'=>array('first'=>array('value'=>urlencode("您的活動已通過"),'color'=>"#FF0000"),'keyword1'=>array('value'=>urlencode('測試文章標題'),'color'=>'#FF0000'), //keyword需要與配置的模板消息對應'keyword2'=>array('value'=>urlencode(date("Y-m-d H:i:s")),'color'=>'#FF0000'),'keyword3'=>array('value'=>urlencode('測試發布人'),'color'=>'#FF0000'),'keyword4'=>array('value'=>urlencode('測試狀態'),'color'=>'#FF0000'),'remark' =>array('value'=>urlencode('備注:這是測試'),'color'=>'#FF0000'), )];$json_template=json_encode($template);return $json_template;}/*** @param $url* @param array $data* @return mixed* curl請求*/function curl_post($url , $data=array()){$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);// POST數據curl_setopt($ch, CURLOPT_POST, 1);// 把post的變量加上curl_setopt($ch, CURLOPT_POSTFIELDS, $data);$output = curl_exec($ch);curl_close($ch);return $output;}最后,謝謝觀看,有不足之處可私信。
總結
以上是生活随笔為你收集整理的微信公众号发送模板消息 -- PHP后台的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网络渗透技术如何自学,自学黑客要多久
- 下一篇: PHP后台管理登录界面代码