Yii Framework框架開發(fā)微信公眾平臺示例
本文實(shí)例講述了Yii Framework框架開發(fā)微信公眾平臺。分享給大家供大家參考,具體如下:
1. 先到微信公眾平臺注冊帳號
http://mp.weixin.qq.com
2. 下載demo
微信公眾平臺提供了一個十分“樸素”的demo,說明如何調(diào)用消息接口的。代碼真的很樸素,具體內(nèi)容可到官網(wǎng)下載。
3. 按照Yii的規(guī)則,做一個extension。
這里命名為 weixin,目錄結(jié)構(gòu)如下:
▾ extensions/
▾ weixin/
Weixin.php*
Weixin.php代碼內(nèi)容:
<?php /** * WeixinCallback * * @package * @version $id$ * @copyright 1997-2005 The PHP Group * @author davidhhuan@126.com * {@link <a rel="external nofollow" target="_blank">http://www.sharefamily.net</a>} */ class Weixin { //$_GET參數(shù) public $signature; public $timestamp; public $nonce; public $echostr; // public $token; public $debug = false; public $msg = array(); public $setFlag = false; /** * __construct * * @param mixed $params * @access public * @return void */ public function __construct($params) { foreach ($params as $k1 => $v1) { if (property_exists($this, $k1)) { $this->$k1 = $v1; } } } /** * valid * * @access public * @return void */ public function valid() { //valid signature , option if($this->checkSignature()){ echo $this->echostr; Yii::app()->end(); } } /** * 獲得用戶發(fā)過來的消息(消息內(nèi)容和消息類型 ) * * @access public * @return void */ public function init() { $postStr = empty($GLOBALS["HTTP_RAW_POST_DATA"]) ? '' : $GLOBALS["HTTP_RAW_POST_DATA"]; if ($this->debug) { $this->log($postStr); } if (!empty($postStr)) { $this->msg = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); } } /** * makeEvent * * @access public * @return void */ public function makeEvent() { } /** * 回復(fù)文本消息 * * @param string $text * @access public * @return void */ 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ù)圖文消息 * * @param array $newsData * @access public * @return void */ 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> <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']); //微信公眾平臺圖文回復(fù)的消息一次最多10條 $itemsCount = $itemsCount < 10 ? $itemsCount : 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,$itemsCount); $footer = sprintf($newTplFoot,$funcFlag); return $header . $content . $footer; } /** * reply * * @param mixed $data * @access public * @return void */ public function reply($data) { if ($this->debug) { $this->log($data); } echo $data; } /** * checkSignature * * @access private * @return void */ private function checkSignature() { $tmpArr = array($this->token, $this->timestamp, $this->nonce); sort($tmpArr); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $this->signature ){ return true; }else{ return false; } } /** * log * * @access private * @return void */ private function log($log) { if ($this->debug) { file_put_contents(Yii::getPathOfAlias('application').'/runtime/weixin_log.txt', var_export($log, true)."\n\r", FILE_APPEND); } } }
使用方法,這里舉例在SiteController里面
/** * actionIndex * * @access public * @return void */ public function actionIndex() { $weixin = new Weixin($_GET); $weixin->token = $this->_weixinToken; //$weixin->debug = true; //網(wǎng)址接入時使用 if (isset($_GET['echostr'])) { $weixin->valid(); } $weixin->init(); $reply = ''; $msgType = empty($weixin->msg->MsgType) ? '' : strtolower($weixin->msg->MsgType); switch ($msgType) { case 'text': //你要處理文本消息代碼 break; case 'image': //你要處理圖文消息代碼 break; case 'location': //你要處理位置消息代碼 break; case 'link': //你要處理鏈接消息代碼 break; case 'event': //你要處理事件消息代碼 break; default: //無效消息情況下的處理方式 break; } $weixin->reply($reply); }
至此,基本的邏輯都實(shí)現(xiàn)了
更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結(jié)》、《php優(yōu)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Yii框架的PHP程序設(shè)計有所幫助。
- 記錄Yii2框架開發(fā)微信公眾號遇到的問題及解決方法
- 使用YII2框架實(shí)現(xiàn)微信公眾號中表單提交功能
- 完美利用Yii2微信后臺開發(fā)的系列總結(jié)
- Yii PHP Framework實(shí)用入門教程(詳細(xì)介紹)
- YII Framework框架使用YIIC快速創(chuàng)建YII應(yīng)用之migrate用法實(shí)例詳解
- YII Framework框架教程之使用YIIC快速創(chuàng)建YII應(yīng)用詳解
- Yii Framework框架獲取分類下面的所有子類方法
- YiiFramework入門知識點(diǎn)總結(jié)(圖文教程)
- YII Framework框架教程之緩存用法詳解
- YII Framework框架教程之國際化實(shí)現(xiàn)方法
- YII Framework框架教程之安全方案詳解
相關(guān)文章
Base64在線編碼解碼實(shí)現(xiàn)代碼 演示與下載
最近遇到的幾個程序的加密方式都是Base64加密,很是暈菜,臨時整了個在線轉(zhuǎn)換,但是也不全部能搞定,呵呵也許還有不行的,希望對后來人有所幫助。2011-01-01Yii框架實(shí)現(xiàn)的驗(yàn)證碼、登錄及退出功能示例
這篇文章主要介紹了Yii框架實(shí)現(xiàn)的驗(yàn)證碼、登錄及退出功能,結(jié)合具體實(shí)例形式分析了基于Yii框架實(shí)現(xiàn)的用戶驗(yàn)證登錄及退出操作相關(guān)步驟與操作技巧,需要的朋友可以參考下2017-05-05Laravel框架FormRequest中重寫錯誤處理的方法
這篇文章主要介紹了Laravel框架FormRequest中重寫錯誤處理的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02thinkPHP3.2.3結(jié)合Laypage實(shí)現(xiàn)的分頁功能示例
這篇文章主要介紹了thinkPHP3.2.3結(jié)合Laypage實(shí)現(xiàn)的分頁功能,結(jié)合實(shí)例形式分析了thinkPHP3.2.3結(jié)合Laypage實(shí)現(xiàn)分頁的model控制器與view視圖相關(guān)操作技巧,需要的朋友可以參考下2018-05-05