当前位置:首页 > CMS教程 > 其它CMS > 列表

Yii Framework框架开发微信公众平台示例

发布:smiling 来源: PHP粉丝网  添加日期:2022-03-02 09:44:57 浏览: 评论:0 

本文实例讲述了Yii Framework框架开发微信公众平台,分享给大家供大家参考,具体如下:

1. 先到微信公众平台注册帐号

http://mp.weixin.qq.com

2. 下载demo

微信公众平台提供了一个十分“朴素”的demo,说明如何调用消息接口的。代码真的很朴素,具体内容可到官网下载。

3. 按照Yii的规则,做一个extension。

这里命名为 weixin,目录结构如下:

  1. ▾ extensions/ 
  2.       ▾ weixin/ 
  3.           Weixin.php* 

Weixin.php代码内容:

  1. <?php 
  2.    
  3. /** 
  4.  * WeixinCallback  
  5.  *  
  6.  * @package  
  7.  * @version $id$ 
  8.  * @copyright 1997-2005 The PHP Group 
  9.  * @author davidhhuan@126.com 
  10.  * {@link <a href="http://www.sharefamily.net" rel="external nofollow" target="_blank">http://www.sharefamily.net</a>} 
  11.  */ 
  12. class Weixin 
  13. { 
  14.   //$_GET参数 
  15.   public $signature; 
  16.   public $timestamp; 
  17.   public $nonce; 
  18.   public $echostr; 
  19.   // 
  20.   public $token; 
  21.   public $debug = false; 
  22.   public $msg = array(); 
  23.   public $setFlag = false; 
  24.    
  25.   /** 
  26.    * __construct  
  27.    *  
  28.    * @param mixed $params  
  29.    * @access public 
  30.    * @return void 
  31.    */ 
  32.   public function __construct($params) 
  33.   { 
  34.     foreach ($params as $k1 => $v1) 
  35.     { 
  36.       if (property_exists($this, $k1)) 
  37.       { 
  38.         $this->$k1 = $v1; 
  39.       } 
  40.     } 
  41.   } 
  42.      
  43.   /** 
  44.    * valid  
  45.    *  
  46.    * @access public 
  47.    * @return void 
  48.    */ 
  49.   public function valid() 
  50.   { 
  51.     //valid signature , option 
  52.     if($this->checkSignature()){ 
  53.       echo $this->echostr; 
  54.       Yii::app()->end(); 
  55.     } 
  56.   } 
  57.    
  58.   /** 
  59.    * 获得用户发过来的消息(消息内容和消息类型 )  
  60.    *  
  61.    * @access public 
  62.    * @return void 
  63.    */ 
  64.   public function init() 
  65.   { 
  66.     $postStr = emptyempty($GLOBALS["HTTP_RAW_POST_DATA"]) ? '' : $GLOBALS["HTTP_RAW_POST_DATA"]; 
  67.     if ($this->debug)  
  68.     { 
  69.       $this->log($postStr); 
  70.     } 
  71.     if (!emptyempty($postStr)) { 
  72.       $this->msg = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); 
  73.     } 
  74.   } 
  75.    
  76.   /** 
  77.    * makeEvent  
  78.    *  
  79.    * @access public 
  80.    * @return void 
  81.    */ 
  82.   public function makeEvent() 
  83.   { 
  84.        
  85.   } 
  86.    
  87.   /** 
  88.    * 回复文本消息  
  89.    *  
  90.    * @param string $text  
  91.    * @access public 
  92.    * @return void 
  93.    */ 
  94.   public function makeText($text='') 
  95.   { 
  96.     $createTime = time(); 
  97.     $funcFlag = $this->setFlag ? 1 : 0; 
  98.     $textTpl = "<xml> 
  99.       <ToUserName><![CDATA[{$this->msg->FromUserName}]]></ToUserName> 
  100.       <FromUserName><![CDATA[{$this->msg->ToUserName}]]></FromUserName> 
  101.       <CreateTime>{$createTime}</CreateTime> 
  102.       <MsgType><![CDATA[text]]></MsgType> 
  103.       <Content><![CDATA[%s]]></Content> 
  104.       <FuncFlag>%s</FuncFlag> 
  105.       </xml>"; 
  106.     return sprintf($textTpl,$text,$funcFlag); 
  107.   } 
  108.      
  109.   /** 
  110.    * 根据数组参数回复图文消息  
  111.    *  
  112.    * @param array $newsData  
  113.    * @access public 
  114.    * @return void 
  115.    */ 
  116.   public function makeNews($newsData=array()) 
  117.   { 
  118.     $createTime = time(); 
  119.     $funcFlag = $this->setFlag ? 1 : 0; 
  120.     $newTplHeader = "<xml> 
  121.       <ToUserName><![CDATA[{$this->msg->FromUserName}]]></ToUserName> 
  122.       <FromUserName><![CDATA[{$this->msg->ToUserName}]]></FromUserName> 
  123.       <CreateTime>{$createTime}</CreateTime> 
  124.       <MsgType><![CDATA[news]]></MsgType> 
  125.       <ArticleCount>%s</ArticleCount><Articles>"; 
  126.     $newTplItem = "<item> 
  127.       <Title><![CDATA[%s]]></Title> 
  128.       <Description><![CDATA[%s]]></Description> 
  129.       <PicUrl><![CDATA[%s]]></PicUrl> 
  130.       <Url><![CDATA[%s]]></Url> 
  131.       </item>"; 
  132.     $newTplFoot = "</Articles> 
  133.       <FuncFlag>%s</FuncFlag> 
  134.       </xml>"; 
  135.     $content = ''; 
  136.     $itemsCount = count($newsData['items']); 
  137.     //微信公众平台图文回复的消息一次最多10条 
  138.     $itemsCount = $itemsCount < 10 ? $itemsCount : 10; 
  139.     if ($itemsCount) { 
  140.       foreach ($newsData['items'] as $key => $item) { 
  141.         if ($key<=9) { 
  142.           $content .= sprintf($newTplItem,$item['title'],$item['description'],$item['picurl'],$item['url']); 
  143.         } 
  144.       } 
  145.     } 
  146.     $header = sprintf($newTplHeader,$itemsCount); 
  147.     $footer = sprintf($newTplFoot,$funcFlag); 
  148.     return $header . $content . $footer; 
  149.   } 
  150.    
  151.   /** 
  152.    * reply  
  153.    *  
  154.    * @param mixed $data  
  155.    * @access public 
  156.    * @return void 
  157.    */ 
  158.   public function reply($data) 
  159.   { 
  160.     if ($this->debug)  
  161.     { 
  162.       $this->log($data); 
  163.     } 
  164.     echo $data; 
  165.   } 
  166.    
  167.   /** 
  168.    * checkSignature  
  169.    *  
  170.    * @access private 
  171.    * @return void 
  172.    */ 
  173.   private function checkSignature() 
  174.   { 
  175.     $tmpArr = array($this->token, $this->timestamp, $this->nonce); 
  176.     sort($tmpArr); 
  177.     $tmpStr = implode( $tmpArr ); 
  178.     $tmpStr = sha1( $tmpStr ); 
  179.        
  180.     if( $tmpStr == $this->signature ){ 
  181.       return true; 
  182.     }else{ 
  183.       return false; 
  184.     } 
  185.   } 
  186.    
  187.   /** 
  188.    * log  
  189.    *  
  190.    * @access private 
  191.    * @return void 
  192.    */ 
  193.   private function log($log) 
  194.   { 
  195.     if ($this->debug) 
  196.     { 
  197.       file_put_contents(Yii::getPathOfAlias('application').'/runtime/weixin_log.txt', var_export($log, true)."\n\r", FILE_APPEND); 
  198.     } 
  199.   } 
  200. } 

使用方法,这里举例在SiteController里面。

  1. /** 
  2.    * actionIndex  
  3.    *  
  4.    * @access public 
  5.    * @return void 
  6.    */ 
  7.   public function actionIndex() 
  8.   { 
  9.     $weixin = new Weixin($_GET); 
  10.     $weixin->token = $this->_weixinToken; 
  11.     //$weixin->debug = true; 
  12.    
  13.     //网址接入时使用 
  14.     if (isset($_GET['echostr'])) 
  15.     { 
  16.       $weixin->valid(); 
  17.     } 
  18.        
  19.     $weixin->init(); 
  20.     $reply = ''; 
  21.     $msgType = emptyempty($weixin->msg->MsgType) ? '' : strtolower($weixin->msg->MsgType); 
  22.     switch ($msgType) 
  23.     { 
  24.     case 'text': 
  25.       //你要处理文本消息代码 
  26.       break; 
  27.     case 'image': 
  28.       //你要处理图文消息代码 
  29.       break; 
  30.     case 'location': 
  31.       //你要处理位置消息代码 
  32.       break; 
  33.     case 'link': 
  34.       //你要处理链接消息代码 
  35.       break; 
  36.     case 'event': 
  37.       //你要处理事件消息代码 
  38.       break; 
  39.     default:  
  40.       //无效消息情况下的处理方式 
  41.       break; 
  42.     } 
  43.     $weixin->reply($reply); 
  44.   } 

至此,基本的逻辑都实现了。

Tags: Framework Yii开发微信公众平台

分享到: