久久久久在线观看_又色又爽又黄的免费视频播放_一区中文字幕_日韩电影在线播放

今日頭條 焦點資訊 營銷之道 企業(yè)報道 淘寶運營 網站建設 軟件開發(fā) 400電話
  當前位置: 首頁 » 資訊 » 軟件開發(fā) » 正文

PHP實現(xiàn)微信模板消息示例

放大字體  縮小字體 發(fā)布日期:2018-02-10  來源:企業(yè)800網  作者:新格網  瀏覽次數(shù):655  【去百度看看】
本文主要和大家介紹了PHP微信模板消息操作方法,結合實例形式分析了php模板消息的定義與調用方法,需要的朋友可以參考下,希望能幫助到大家。

微信SDK:


<?php
class Oauth {
  //獲得全局access_token
  public function get_token(){
  //如果已經存在直接返回access_token
    //if($_SESSION['access_token'] && $_SESSION['expire_time']>time()){
      //return $_SESSION['access_token'];
    //}else{
    //1.請求url地址
    $appid = APPID;  //appid
    $appsecret = APPSECRET;  //appsecret
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret; //請求地址
    //2初始化curl請求
    $ch = curl_init();
    //3.配置請求參數(shù)
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳過證書檢查
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 從證書中檢查SSL加密算法是否存在
    curl_setopt($ch, CURLOPT_URL, $url);  //請求
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //不直接輸出數(shù)據(jù)
    //4.開始請求
    $res = curl_exec($ch); //獲取請求結果
    if( curl_errno($ch) ){
      var_dump( curl_error($ch) ); //打印錯誤信息
    }
    //5.關閉curl
    curl_close( $ch );
    $arr = json_decode($res, true); //將結果轉為數(shù)組
    //$_SESSION['access_token']=$arr['access_token'];  //將access_token存入session中,可以不存,每次都獲得新的token
    //$_SESSION['expire_time']=time()+7200;
    return $arr['access_token'];
    //}
  }
  //推送模板信息  參數(shù):發(fā)送給誰的openid,客戶姓名,客戶電話,推薦樓盤(參數(shù)自定)
  function sendMessage($openid,$customName,$customPhone,$reportBuilding) {
    //獲取全局token
    $token = $this->get_token();
    $url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$token; //模板信息請求地址
    //發(fā)送的模板信息(微信要求json格式,這里為數(shù)組(方便添加變量)格式,然后轉為json)
    $post_data = array(
        "touser"=>$openid, //推送給誰,openid
        "template_id"=>"nKu4eyktzxOslxq0KfPxhGXbiOo873K9mIxKvs23EVU", //微信后臺的模板信息id
        "url"=>"http://www.baidu.com", //下面為預約看房模板示例
        "data"=> array(
            "first" => array(
                "value"=>"您有新客戶,請及時查看!",
                "color"=>"#173177"
            ),
            "customName"=>array(
                "value"=>$customName, //傳的變量
                "color"=>"#173177"
            ),
            "customPhone"=>array(
                "value"=>$customPhone,
                "color"=>"#173177"
            ),
            "reportBuilding"=> array(
                "value"=>$reportBuilding,
                "color"=>"#173177"
            ),
            "reportTime"=> array(
                "value"=>date('Y-m-d H:i:s'),
                "color"=>"#173177"
            ),
            "remark"=> array(
                "value"=>"請及時聯(lián)系客戶哦!",
                "color"=>"#173177"
            ),
        )
    );
    //將上面的數(shù)組數(shù)據(jù)轉為json格式
    $post_data = json_encode($post_data);
    //發(fā)送數(shù)據(jù),post方式
    //配置curl請求
    $ch = curl_init();  //創(chuàng)建curl請求
    curl_setopt($ch, CURLOPT_URL,$url); //設置發(fā)送數(shù)據(jù)的網址
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //設置有返回值,0,直接顯示
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0); //禁用證書驗證
    curl_setopt($ch, CURLOPT_POST, 1);  //post方法請求
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);//post請求發(fā)送的數(shù)據(jù)包
    //接收執(zhí)行返回的數(shù)據(jù)
    $data = curl_exec($ch);
    //關閉句柄
    curl_close($ch);
    $data = json_decode($data,true); //將json數(shù)據(jù)轉成數(shù)組
    return $data;
  }
  //獲取模板信息-行業(yè)信息(參考,示例未使用)
  function getHangye(){
    //用戶同意授權后,會傳過來一個code
    $token = $this->get_token();
    $url = "https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token=".$token;
    //請求token,get方式
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
    $data = curl_exec($ch);
    curl_close($ch);
    $data = json_decode($data,true); //將json數(shù)據(jù)轉成數(shù)組
    //return $data["access_token"];
    return $data;
  }
}

PHP代碼:


//推送模板信息給置業(yè)顧問
$send = new Oauth();  //實例化類
$send->sendMessage($zhiyeguwen,$clientName,$tel,$product);  //調用方法

完成,微信模板信息不難,有問題互相交流。。

相關推薦:

php實現(xiàn)發(fā)送微信模板消息的方法,php信模板消息_PHP教程

ThinkPHP3.2.3實現(xiàn)推送微信模板消息

微信模板消息調用

以上就是PHP實現(xiàn)微信模板消息示例的詳細內容,更多請關注php中文網其它相關文章!

 
關鍵詞: php,示例,消息
 
[ 資訊搜索 ]  [ 加入收藏 ]  [ 告訴好友 ]  [ 打印本文 ]  [ 違規(guī)舉報 ]  [ 關閉窗口 ]

 
0條 [查看全部]  相關評論

 
網站首頁 | 關于我們 | 聯(lián)系方式 | 使用協(xié)議 | 版權隱私 | 網站地圖 | 排名推廣 | 廣告服務 | 積分換禮 | 網站留言 | RSS訂閱 | 吉ICP備11001726號-6
企業(yè)800網 · 提供技術支持