php微信公眾號(hào)開(kāi)發(fā)(3)php實(shí)現(xiàn)簡(jiǎn)單微信文本通訊
微信開(kāi)發(fā)前,需要設(shè)置token,這個(gè)是微信設(shè)置的,可以任意設(shè)置,用來(lái)實(shí)現(xiàn)微信通訊。這里有一個(gè)別人寫的微信類,功能還比較不錯(cuò)。weixin.class.php代碼如下
<?php class Weixin { public $token = '';//token public $debug = false;//是否debug的狀態(tài)標(biāo)示,方便我們?cè)谡{(diào)試的時(shí)候記錄一些中間數(shù)據(jù) public $setFlag = false; public $msgtype = 'text'; //('text','image','location') public $msg = array(); public function __construct($token,$debug) { $this->token = $token; $this->debug = $debug; } //獲得用戶發(fā)過(guò)來(lái)的消息(消息內(nèi)容和消息類型 ) public function getMsg() { $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!empty($postStr)) { $this->msg = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $this->msgtype = strtolower($this->msg['MsgType']); } } //回復(fù)文本消息 public function makeText($text='') { $CreateTime = time(); $FuncFlag = $this->setFlag ? 1 : 0; $textTpl = "<xml> <ToUserName><![CDATA[{$this->msg['FromUserName']}]]></ToUserName> <FromUserName><![CDATA[{$this->msg['ToUserName']}]]></FromUserName> <CreateTime>{$CreateTime}</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>%s</FuncFlag> </xml>"; return sprintf($textTpl,$text,$FuncFlag); } //根據(jù)數(shù)組參數(shù)回復(fù)圖文消息 public function makeNews($newsData=array()) { $CreateTime = time(); $FuncFlag = $this->setFlag ? 1 : 0; $newTplHeader = "<xml> <ToUserName><![CDATA[{$this->msg['FromUserName']}]]></ToUserName> <FromUserName><![CDATA[{$this->msg['ToUserName']}]]></FromUserName> <CreateTime>{$CreateTime}</CreateTime> <MsgType><![CDATA[news]]></MsgType> <Content><![CDATA[%s]]></Content> <ArticleCount>%s</ArticleCount><Articles>"; $newTplItem = "<item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item>"; $newTplFoot = "</Articles> <FuncFlag>%s</FuncFlag> </xml>"; $Content = ''; $itemsCount = count($newsData['items']); $itemsCount = $itemsCount < 10 ? $itemsCount : 10;//微信公眾平臺(tái)圖文回復(fù)的消息一次最多10條 if ($itemsCount) { foreach ($newsData['items'] as $key => $item) { if ($key<=9) { $Content .= sprintf($newTplItem,$item['title'],$item['description'],$item['picurl'],$item['url']); } } } $header = sprintf($newTplHeader,$newsData['content'],$itemsCount); $footer = sprintf($newTplFoot,$FuncFlag); return $header . $Content . $footer; } public function reply($data) { echo $data; } public function valid() { if ($this->checkSignature()) { if( $_SERVER['REQUEST_METHOD']=='GET' ) { echo $_GET['echostr']; exit; } }else{ exit; } } private function checkSignature() { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $tmpArr = array($this->token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } } ?>
接著正式開(kāi)發(fā),使用百度SVN地址,創(chuàng)建weixinapi.php文件,這個(gè)根據(jù)你后臺(tái)設(shè)置名稱。
<?php define("TOKEN", ""); define('DEBUG', false); include_once('weixin.class.php'); require_once("db.php"); $weixin = new Weixin(TOKEN,DEBUG);//實(shí)例化 $weixin->getMsg(); $type = $weixin->msgtype;//消息類型 $keyword = $weixin->msg['Content'];//獲取的文本 if ($type==='text') { $reply = $weixin->makeText($key); }elseif($type==='event'){//第一次關(guān)注推送事件 $reply = $weixin->makeText("歡迎關(guān)注"); }else{//其他類型 $reply = $weixin->makeText("暫時(shí)沒(méi)有圖片,聲音,地理位置等功能,后續(xù)開(kāi)發(fā)會(huì)增加,感謝你關(guān)注"); } $weixin->reply($reply);
這樣就實(shí)現(xiàn)了一個(gè)例子,第一次關(guān)注事件回復(fù),非文本回復(fù),以及文本回復(fù),這里文本回復(fù)是你輸入什么就返回什么。
具體實(shí)現(xiàn)功能就寫在文本回復(fù)里面。
其他的功能暫時(shí)不做,具體開(kāi)發(fā)下節(jié)再說(shuō)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 微信公眾平臺(tái)開(kāi)發(fā)——群發(fā)信息
- php微信公眾平臺(tái)開(kāi)發(fā)之微信群發(fā)信息
- php微信高級(jí)接口群發(fā) 多客服
- C#微信公眾平臺(tái)開(kāi)發(fā)之高級(jí)群發(fā)接口
- asp.net微信開(kāi)發(fā)(高級(jí)群發(fā)圖文)
- asp.net微信開(kāi)發(fā)(高級(jí)群發(fā)文本)
- php實(shí)現(xiàn)微信公眾號(hào)無(wú)限群發(fā)
- C#實(shí)現(xiàn)微信公眾號(hào)群發(fā)消息(解決一天只能發(fā)一次的限制)實(shí)例分享
- php微信公眾號(hào)開(kāi)發(fā)(4)php實(shí)現(xiàn)自定義關(guān)鍵字回復(fù)
- 微信公眾號(hào)模板消息群發(fā)php代碼示例
相關(guān)文章
thinkPHP5框架閉包函數(shù)與子查詢傳參用法示例
這篇文章主要介紹了thinkPHP5框架閉包函數(shù)與子查詢傳參用法,結(jié)合實(shí)例形式分析了thinkPHP5閉包查詢與參數(shù)傳遞相關(guān)操作技巧,需要的朋友可以參考下2018-08-08微信公眾號(hào)實(shí)現(xiàn)會(huì)員卡領(lǐng)取功能
這篇文章主要介紹了微信公眾號(hào)實(shí)現(xiàn)會(huì)員卡領(lǐng)取功能的相關(guān)資料,需要的朋友可以參考下2017-06-06PHP+Redis事務(wù)解決高并發(fā)下商品超賣問(wèn)題(推薦)
這篇文章主要介紹了PHP+Redis事務(wù)解決高并發(fā)下商品超賣問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08PHP框架實(shí)現(xiàn)WebSocket在線聊天通訊系統(tǒng)
這篇文章主要介紹了PHP框架結(jié)合實(shí)現(xiàn)WebSocket在線聊天通訊系統(tǒng),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11解決laravel id非自增 模型取回為0 的問(wèn)題
今天小編就為大家分享一篇解決laravel id非自增 模型取回為0 的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10codeigniter數(shù)據(jù)庫(kù)操作函數(shù)匯總
網(wǎng)上倒是有不少Codeigniter數(shù)據(jù)庫(kù)操作的介紹,這里做一個(gè)匯總,需要的朋友可以參考下2014-06-06