佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > php實(shí)現(xiàn)微信模板消息推送

php實(shí)現(xiàn)微信模板消息推送

熱門標(biāo)簽:科大訊飛語音識別系統(tǒng) 服務(wù)器配置 Mysql連接數(shù)設(shè)置 電子圍欄 Linux服務(wù)器 銀行業(yè)務(wù) 團(tuán)購網(wǎng)站 阿里云

本文實(shí)例為大家分享了php微信模板消息推送的具體代碼,供大家參考,具體內(nèi)容如下

1.微信公眾號模板消息配置

2.PHP代碼

/**
* 發(fā)送模板消息
*/
 public function send_notice(){
  //獲取access_token
  if ($_COOKIE['access_token']){
    $access_token2=$_COOKIE['access_token'];
  }else{
    $json_token=$this>curl_post("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credentialappid='.$appid.'secret='.$appsecret.'");
  $access_token1=json_decode($json_token,true);
  $access_token2=$access_token1['access_token'];
  setcookie('access_token',$access_token2,7200);
  }
   //模板消息
   $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 '發(fā)送成功';
   }else{
     return '發(fā)送失敗';
   }
  }

  /**
   * 將模板消息json格式化
   */
  public function json_tempalte(){
    //模板消息
    $template=array(
      'touser'=>'.$openid.',  //用戶openid
      'template_id'=>".$tenpalate_id.", //在公眾號下配置的模板id
      'url'=>".$uel.", //點(diǎn)擊模板消息會跳轉(zhuǎn)的鏈接
      'topcolor'=>"#7B68EE",
      'data'=>array(
        'first'=>array('value'=>urlencode("您的活動已通過"),'color'=>"#FF0000"),
        'keyword1'=>array('value'=>urlencode('測試文章標(biāo)題'),'color'=>'#FF0000'),  //keyword需要與配置的模板消息對應(yīng)
        'keyword2'=>array('value'=>urlencode(date("Y-m-d H:i:s")),'color'=>'#FF0000'),
        'keyword3'=>array('value'=>urlencode('測試發(fā)布人'),'color'=>'#FF0000'),
        'keyword4'=>array('value'=>urlencode('測試狀態(tài)'),'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數(shù)據(jù)
    curl_setopt($ch, CURLOPT_POST, 1);
    // 把post的變量加上
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
  }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • 詳解php微信小程序消息推送配置
  • php基于Redis消息隊列實(shí)現(xiàn)的消息推送的方法
  • php實(shí)現(xiàn)websocket實(shí)時消息推送
  • 微信小程序 消息推送php服務(wù)器驗證實(shí)例詳解
  • 解析php做推送服務(wù)端實(shí)現(xiàn)ios消息推送
  • php之a(chǎn)pp消息推送案例教程

標(biāo)簽:衡水 衢州 萍鄉(xiāng) 棗莊 廣元 江蘇 大理 蚌埠

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《php實(shí)現(xiàn)微信模板消息推送》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    武乡县| 蚌埠市| 娄烦县| 图木舒克市| 徐州市| 历史| 手游| 鹤庆县| 泗洪县| 沐川县| 剑河县| 延吉市| 承德县| 商城县| 含山县| 玉龙| 崇义县| 尚义县| 彝良县| 江口县| 车险| 定南县| 巴南区| 西乌| 闽侯县| 斗六市| 城固县| 仁怀市| 临沂市| 海原县| 道孚县| 彰化县| 丹巴县| 涪陵区| 江川县| 太和县| 古丈县| 临沭县| 邵阳县| 武城县| 中山市|