這篇文章主要介紹了PHP模板訊息作業方法,結合執行個體形式分析了php模板訊息的定義與調用方法,需要的朋友可以參考下
本文執行個體講述了PHP模板訊息作業方法。分享給大家供大家參考,具體如下:
SDK:
<?phpclass 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.配置請求參數 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); //不直接輸出資料 //4.開始請求 $res = curl_exec($ch); //擷取請求結果 if( curl_errno($ch) ){ var_dump( curl_error($ch) ); //列印錯誤資訊 } //5.關閉curl curl_close( $ch ); $arr = json_decode($res, true); //將結果轉為數組 //$_SESSION['access_token']=$arr['access_token']; //將access_token存入session中,可以不存,每次都獲得新的token //$_SESSION['expire_time']=time()+7200; return $arr['access_token']; //} } //推送模板資訊 參數:發送給誰的openid,客戶姓名,客戶電話,推薦樓盤(參數自定) 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; //模板資訊請求地址 //發送的模板資訊(要求json格式,這裡為數組(方便添加變數)格式,然後轉為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"=>"請及時聯絡客戶哦!", "color"=>"#173177" ), ) ); //將上面的數組資料轉為json格式 $post_data = json_encode($post_data); //發送資料,post方式 //配置curl請求 $ch = curl_init(); //建立curl請求 curl_setopt($ch, CURLOPT_URL,$url); //設定發送資料的網址 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請求發送的資料包 //接收執行返回的資料 $data = curl_exec($ch); //關閉控制代碼 curl_close($ch); $data = json_decode($data,true); //將json資料轉成數組 return $data; } //擷取模板資訊-行業資訊(參考,樣本未使用) 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資料轉成數組 //return $data["access_token"]; return $data; }}
PHP代碼:
//推送模板資訊給置業顧問$send = new Oauth(); //執行個體化類$send->sendMessage($zhiyeguwen,$clientName,$tel,$product); //調用方法
完成,模板資訊不難,有問題互相交流!!!