您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“如何操作Yii Framework框架開發(fā)微信公眾平臺(tái)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“如何操作Yii Framework框架開發(fā)微信公眾平臺(tái)”吧!
1. 先到微信公眾平臺(tái)注冊(cè)帳號(hào)
2. 下載demo
微信公眾平臺(tái)提供了一個(gè)十分“樸素”的demo,說明如何調(diào)用消息接口的。代碼真的很樸素,具體內(nèi)容可到官網(wǎng)下載。
3. 按照Yii的規(guī)則,做一個(gè)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 href="http://www.sharefamily.net" 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']); //微信公眾平臺(tái)圖文回復(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)址接入時(shí)使用 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: //無(wú)效消息情況下的處理方式 break; } $weixin->reply($reply); }
至此,基本的邏輯都實(shí)現(xiàn)了
到此,相信大家對(duì)“如何操作Yii Framework框架開發(fā)微信公眾平臺(tái)”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。