php實(shí)現(xiàn)發(fā)送微信模板消息的方法
本文實(shí)例講述了php實(shí)現(xiàn)發(fā)送微信模板消息的方法。分享給大家供大家參考。具體如下:
該方法基于thinkphp實(shí)現(xiàn)實(shí)現(xiàn),具體OrderPush.class.php文件如下:
namespace Org\Weixin;
/**
* Created by PhpStorm.
* User: StandOpen
* Date: 15-1-7
* Time: 9:41
*/
class OrderPush
{
protected $appid;
protected $secrect;
protected $accessToken;
function __construct($appid, $secrect)
{
$this->appid = $appid;
$this->secrect = $secrect;
$this->accessToken = $this->getToken($appid, $secrect);
}
/**
* 發(fā)送post請(qǐng)求
* @param string $url
* @param string $param
* @return bool|mixed
*/
function request_post($url = '', $param = '')
{
if (empty($url) || empty($param)) {
return false;
}
$postUrl = $url;
$curlPost = $param;
$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_URL, $postUrl); //抓取指定網(wǎng)頁
curl_setopt($ch, CURLOPT_HEADER, 0); //設(shè)置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求結(jié)果為字符串且輸出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch); //運(yùn)行curl
curl_close($ch);
return $data;
}
/**
* 發(fā)送get請(qǐng)求
* @param string $url
* @return bool|mixed
*/
function request_get($url = '')
{
if (empty($url)) {
return false;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
/**
* @param $appid
* @param $appsecret
* @return mixed
* 獲取token
*/
protected function getToken($appid, $appsecret)
{
if (S($appid)) {
$access_token = S($appid);
} else {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret;
$token = $this->request_get($url);
$token = json_decode(stripslashes($token));
$arr = json_decode(json_encode($token), true);
$access_token = $arr['access_token'];
S($appid, $access_token, 720);
}
return $access_token;
}
/**
* 發(fā)送自定義的模板消息
* @param $touser
* @param $template_id
* @param $url
* @param $data
* @param string $topcolor
* @return bool
*/
public function doSend($touser, $template_id, $url, $data, $topcolor = '#7B68EE')
{
/*
* data=>array(
'first'=>array('value'=>urlencode("您好,您已購(gòu)買成功"),'color'=>"#743A3A"),
'name'=>array('value'=>urlencode("商品信息:微時(shí)代電影票"),'color'=>'#EEEEEE'),
'remark'=>array('value'=>urlencode('永久有效!密碼為:1231313'),'color'=>'#FFFFFF'),
)
*/
$template = array(
'touser' => $touser,
'template_id' => $template_id,
'url' => $url,
'topcolor' => $topcolor,
'data' => $data
);
$json_template = json_encode($template);
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $this->accessToken;
$dataRes = $this->request_post($url, urldecode($json_template));
if ($dataRes['errcode'] == 0) {
return true;
} else {
return false;
}
}
}
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
- php微信公眾號(hào)開發(fā)模式詳解
- php 微信公眾平臺(tái)開發(fā)模式實(shí)現(xiàn)多客服的實(shí)例代碼
- PHP微信公眾號(hào)自動(dòng)發(fā)送紅包API
- 關(guān)于php微信訂閱號(hào)開發(fā)之token驗(yàn)證后自動(dòng)發(fā)送消息給訂閱號(hào)但是沒有消息返回的問題
- PHP實(shí)現(xiàn)微信模擬登陸并給用戶發(fā)送消息的方法【文字,圖片,圖文】
- php版微信公眾平臺(tái)實(shí)現(xiàn)預(yù)約提交后發(fā)送email的方法
- 驗(yàn)證token、回復(fù)圖文\文本、推送消息的實(shí)用微信類php代碼
- PHP memcache在微信公眾平臺(tái)的應(yīng)用方法示例
- PHP實(shí)現(xiàn)微信公眾號(hào)企業(yè)號(hào)自定義菜單接口示例
- 基于php的微信公眾平臺(tái)開發(fā)入門實(shí)例
- PHP使用微信開發(fā)模式實(shí)現(xiàn)搜索已發(fā)送圖文及匹配關(guān)鍵字回復(fù)的方法
相關(guān)文章
淺析PHP程序防止ddos,dns,集群服務(wù)器攻擊的解決辦法
本篇文章是對(duì)PHP程序防止ddos,dns,集群服務(wù)器攻擊的解決辦法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
PHP實(shí)現(xiàn)手機(jī)號(hào)碼中間四位用星號(hào)(*)隱藏的自定義函數(shù)分享
這篇文章主要介紹了PHP實(shí)現(xiàn)手機(jī)號(hào)碼中間四位用星號(hào)(*)隱藏的自定義函數(shù)分享,這是一個(gè)比較常用的功能,需要的朋友可以參考下2014-09-09
php 使用curl模擬ip和來源進(jìn)行訪問的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猵hp 使用curl模擬ip和來源進(jìn)行訪問的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
PHP工廠模式簡(jiǎn)單實(shí)現(xiàn)方法示例
這篇文章主要介紹了PHP工廠模式簡(jiǎn)單實(shí)現(xiàn)方法,簡(jiǎn)單說明了工廠模式的概念、原理并結(jié)合實(shí)例形式分析了php實(shí)現(xiàn)工廠模式的相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
php常用字符串查找函數(shù)strstr()與strpos()實(shí)例分析
這篇文章主要介紹了php常用字符串查找函數(shù)strstr()與strpos(),結(jié)合具體實(shí)例形式分析了php字符串查找函數(shù)strstr()與strpos()的具體功能、用法、區(qū)別及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-06-06
PHP+Oracle本地開發(fā)環(huán)境搭建方法詳解
在本篇文章中小編給大家分享了關(guān)于PHP+Oracle本地開發(fā)環(huán)境搭建的步驟和技巧,需要的朋友們學(xué)習(xí)下。2019-04-04
PHP標(biāo)準(zhǔn)庫(kù)(PHP SPL)詳解
今天小編就為大家分享一篇關(guān)于PHP標(biāo)準(zhǔn)庫(kù)(PHP SPL)詳解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03
php銀聯(lián)網(wǎng)頁支付實(shí)現(xiàn)方法
這篇文章主要介紹了php銀聯(lián)網(wǎng)頁支付實(shí)現(xiàn)方法,實(shí)例分析了php操作銀聯(lián)網(wǎng)支付接口的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03

