《PHP實(shí)戰(zhàn):PHP微信模板消息操作示例》要點(diǎn):
本文介紹了PHP實(shí)戰(zhàn):PHP微信模板消息操作示例,希望對您有用。如果有疑問,可以聯(lián)系我們。
本文實(shí)例講述了PHP微信模板消息操作方法.分享給大家供大家參考,具體如下:PHP實(shí)例
微信SDK:PHP實(shí)例
<?php class Oauth { //獲得全局access_token public function get_token(){ //如果已經(jīng)存在直接返回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); //獲取請求結(jié)果 if( curl_errno($ch) ){ var_dump( curl_error($ch) ); //打印錯(cuò)誤信息 } //5.關(guān)閉curl curl_close( $ch ); $arr = json_decode($res, true); //將結(jié)果轉(zhuǎn)為數(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ù)組(方便添加變量)格式,然后轉(zhuǎn)為json) $post_data = array( "touser"=>$openid, //推送給誰,openid "template_id"=>"nKu4eyktzxOslxq0KfPxhGXbiOo873K9mIxKvs23EVU", //微信后臺的模板信息id "url"=>"http://www.baidu.com", //下面為預(yù)約看房模板示例 "data"=> array( "first" => array( "value"=>"您有新客戶,請及時(shí)查看!", "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"=>"請及時(shí)聯(lián)系客戶哦!", "color"=>"#173177" ), ) ); //將上面的數(shù)組數(shù)據(jù)轉(zhuǎn)為json格式 $post_data = json_encode($post_data); //發(fā)送數(shù)據(jù),post方式 //配置curl請求 $ch = curl_init(); //創(chuàng)建curl請求 curl_setopt($ch, CURLOPT_URL,$url); //設(shè)置發(fā)送數(shù)據(jù)的網(wǎng)址 curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //設(shè)置有返回值,0,直接顯示 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0); //禁用證書驗(yàn)證 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); //關(guān)閉句柄 curl_close($ch); $data = json_decode($data,true); //將json數(shù)據(jù)轉(zhuǎn)成數(shù)組 return $data; } //獲取模板信息-行業(yè)信息(參考,示例未使用) function getHangye(){ //用戶同意授權(quán)后,會傳過來一個(gè)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ù)轉(zhuǎn)成數(shù)組 //return $data["access_token"]; return $data; } }
PHP代碼:PHP實(shí)例
//推送模板信息給置業(yè)顧問 $send = new Oauth(); //實(shí)例化類 $send->sendMessage($zhiyeguwen,$clientName,$tel,$product); //調(diào)用方法
完成,微信模板信息不難,有問題互相交流!!!PHP實(shí)例
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP微信開發(fā)技巧匯總》、《PHP編碼與轉(zhuǎn)碼操作技巧匯總》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語法入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》PHP實(shí)例
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助.PHP實(shí)例
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/557.html