PHP微信模板消息操作示例
本文實(shí)例講述了PHP微信模板消息操作方法。分享給大家供大家參考,具體如下:
微信SDK:
<?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.請(qǐng)求url地址
$appid = APPID; //appid
$appsecret = APPSECRET; //appsecret
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret; //請(qǐng)求地址
//2初始化curl請(qǐng)求
$ch = curl_init();
//3.配置請(qǐng)求參數(shù)
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳過(guò)證書檢查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 從證書中檢查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_URL, $url); //請(qǐng)求
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //不直接輸出數(shù)據(jù)
//4.開(kāi)始請(qǐng)求
$res = curl_exec($ch); //獲取請(qǐng)求結(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ā)送給誰(shuí)的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; //模板信息請(qǐng)求地址
//發(fā)送的模板信息(微信要求json格式,這里為數(shù)組(方便添加變量)格式,然后轉(zhuǎn)為json)
$post_data = array(
"touser"=>$openid, //推送給誰(shuí),openid
"template_id"=>"nKu4eyktzxOslxq0KfPxhGXbiOo873K9mIxKvs23EVU", //微信后臺(tái)的模板信息id
"url"=>"http://www.baidu.com", //下面為預(yù)約看房模板示例
"data"=> array(
"first" => array(
"value"=>"您有新客戶,請(qǐng)及時(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"=>"請(qǐng)及時(shí)聯(lián)系客戶哦!",
"color"=>"#173177"
),
)
);
//將上面的數(shù)組數(shù)據(jù)轉(zhuǎn)為json格式
$post_data = json_encode($post_data);
//發(fā)送數(shù)據(jù),post方式
//配置curl請(qǐng)求
$ch = curl_init(); //創(chuàng)建curl請(qǐng)求
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方法請(qǐng)求
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);//post請(qǐng)求發(fā)送的數(shù)據(jù)包
//接收?qǐng)?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)后,會(huì)傳過(guò)來(lái)一個(gè)code
$token = $this->get_token();
$url = "https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token=".$token;
//請(qǐng)求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代碼:
//推送模板信息給置業(yè)顧問(wèn) $send = new Oauth(); //實(shí)例化類 $send->sendMessage($zhiyeguwen,$clientName,$tel,$product); //調(diào)用方法
完成,微信模板信息不難,有問(wèn)題互相交流!??!
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP微信開(kāi)發(fā)技巧匯總》、《PHP編碼與轉(zhuǎn)碼操作技巧匯總》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語(yǔ)法入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP實(shí)現(xiàn)微信模擬登陸并給用戶發(fā)送消息的方法【文字,圖片,圖文】
- 微信小程序圖片選擇、上傳到服務(wù)器、預(yù)覽(PHP)實(shí)現(xiàn)實(shí)例
- PHP仿微信多圖片預(yù)覽上傳實(shí)例代碼
- PHP開(kāi)發(fā)的微信現(xiàn)金紅包功能示例
- php實(shí)現(xiàn)微信模擬登陸、獲取用戶列表及群發(fā)消息功能示例
- PHP對(duì)接微信公眾平臺(tái)消息接口開(kāi)發(fā)流程教程
- php判斷頁(yè)面是否是微信打開(kāi)的示例(微信打開(kāi)網(wǎng)頁(yè))
- 基于php的微信公眾平臺(tái)開(kāi)發(fā)入門實(shí)例
- php實(shí)現(xiàn)微信公眾平臺(tái)賬號(hào)自定義菜單類
- PHP微信支付開(kāi)發(fā)實(shí)例
- PHP 微信支付類 demo
- PHP實(shí)現(xiàn)微信圖片上傳到服務(wù)器的方法示例
相關(guān)文章
使用ltrace工具跟蹤PHP庫(kù)函數(shù)調(diào)用的方法
這篇文章主要介紹了使用ltrace工具跟蹤PHP庫(kù)函數(shù)調(diào)用的方法,結(jié)合實(shí)例形式分析了ltrace工具用來(lái)跟蹤PHP庫(kù)函數(shù)運(yùn)行時(shí)間的相關(guān)技巧,需要的朋友可以參考下2016-04-04
簡(jiǎn)單的過(guò)濾字符串中的HTML標(biāo)記
簡(jiǎn)單的過(guò)濾字符串中的HTML標(biāo)記...2006-12-12
Linux下進(jìn)行MYSQL編程時(shí)插入中文亂碼的解決方案
Linux下進(jìn)行MYSQL編程時(shí)插入中文亂碼的解決方案...2007-03-03
php獲取mysql數(shù)據(jù)庫(kù)中的所有表名的代碼
如何用PHP獲取MYSQL數(shù)據(jù)庫(kù)的所有表名?記得在mysql命令行下面有條命令SHOW TABLES是顯示mysql數(shù)據(jù)庫(kù)里面所有數(shù)據(jù)表的,那么就用這條命令來(lái)遍歷數(shù)據(jù)表名吧2011-04-04

