Yii Framework框架开发微信公众平台示例
这篇文章主要介绍了Yii Framework框架开发微信公众平台,结合实例形式分析了使用Yii Framework框架开发微信公众平台相关原理、功能代码与实现技巧,需要的朋友可以参考下
本文实例讲述了Yii Framework框架开发微信公众平台。分享给大家供大家参考,具体如下:
1. 先到微信公众平台注册帐号
http://mp.weixin.qq.com
2. 下载demo
微信公众平台提供了一个十分“朴素”的demo,说明如何调用消息接口的。代码真的很朴素,具体内容可到官网下载。
3. 按照Yii的规则,做一个extension。
这里命名为 weixin,目录结构如下:
▾ extensions/
▾ weixin/
Weixin.php*
Weixin.php代码内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | <?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里面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | /** * 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 ); } |
至此,基本的逻辑都实现了