Yii Framework框架开发微信公众平台示例
程序员文章站
2022-03-08 14:12:51
本文实例讲述了yii framework框架开发微信公众平台。分享给大家供大家参考,具体如下:1. 先到微信公众平台注册帐号http://mp.weixin.qq.com2. 下载demo微信公众平台...
本文实例讲述了yii framework框架开发微信公众平台。分享给大家供大家参考,具体如下:
1. 先到微信公众平台注册帐号
http://mp.weixin.qq.com
2. 下载demo
微信公众平台提供了一个十分“朴素”的demo,说明如何调用消息接口的。代码真的很朴素,具体内容可到官网下载。
3. 按照yii的规则,做一个extension。
这里命名为 weixin,目录结构如下:
▾ extensions/
▾ weixin/
weixin.php*
weixin.php代码内容:
<?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参数 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(); } } /** * 获得用户发过来的消息(消息内容和消息类型 ) * * @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() { } /** * 回复文本消息 * * @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); } /** * 根据数组参数回复图文消息 * * @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']); //微信公众平台图文回复的消息一次最多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; //网址接入时使用 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); }
至此,基本的逻辑都实现了