一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

让dedecms增加微信公众平台插件

时间:2013-04-11 编辑:简简单单 来源:一聚教程网

放到dede程序的plus目录下即可.
接口配置信息 按下面说明填写,
URL http://你的域名/plus/weixin.php
Token weixin (和文件中保持一致)

代码如下

 代码如下 复制代码

 
define("TOKEN", "weixin");
require_once(dirname(__FILE__)."/../include/common.inc.php");  
$dsql = new DedeSql(false);
$wechatObj = new wechatCallback();
$wechatObj->valid();
 
class wechatCallback
{
    private $items = '';
    private $articleCount = 0;
    private $keyword = '';
 
 public function valid()
    {
        $echoStr = $_GET["echostr"];
 
        //valid signature , option
        if($this->checkSignature()){
         echo $echoStr;
            $this->responseMsg();
 
         exit;
        }
    }
 
    public function responseMsg()
    {
  //get post data, May be due to the different environments
  $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
 
       //extract post data
  if (!empty($postStr)){
 
               $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $this->keyword = strtolower(trim(iconv("UTF-8","gb2312",$postObj->Content)));
 
                $time = time();
                $textTpl = "
       
       
       ".$time."
       
       
       0
       
";    
                $picTpl = "
                            
                            
                             ".$time."
                            
                            
                             %d
                            
                             %s
                            

                             1
                           
";
                if($this->keyword == 'hi' || $this->keyword == '您好'  || $this->keyword == '你好' ||$this->keyword == 'hello2bizuser' ){
                 $contentStr = "输入关键字开始搜索!";//自定义欢迎回复;
                 echo sprintf($textTpl, $contentStr);
                }else if( !empty( $this->keyword )){
                    $this->search();
                    if($this->articleCount == 0){
                        $contentStr = "抱歉,没有找到与【{$this->keyword}】相关的文章,要不你更换一下关键字,可能就有结果了哦 :-) ";
                        echo sprintf($textTpl, $contentStr);
                    }else{
                        echo sprintf($picTpl,$this->articleCount,$this->items);
                    }
                }
 
        }else {
         echo "";
         exit;
        }
    }
 
    private function search(){
        global $dsql;
        $weixin_posts = $dsql->SetQuery("Select * From `#@__archives` where title like '%".$this->keyword."%' order by id desc LIMIT 0, 5");
        $items = '';
  $dsql->Execute();
 
  while($weixin_post=$dsql->GetObject()){       
            $title =$weixin_post->title;
            $excerpt = $weixin_post->description ;//获取摘要
            $thumb = $weixin_post->litpic ;//获取缩略图;
            $link = '/plus/view.php?aid='.$weixin_post->id;
            $items = $items . $this->get_item($title, $excerpt, $thumb, $link);  
   $this->articleCount++;    
  }
        if($this->articleCount > 5) $this->articleCount = 5;
 
        $this->items = $items;
    }
 
    private function get_item($title, $description, $picUrl, $url){
        if(!$description) $description = $title;
 
        return
        '
       
            <![CDATA['.$title.']]>
           
           
           
       

        ';
    }
 
    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];   
 
        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );
 
        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
}

最终测试结果图;

dedecms 织梦微信公众平台插件

dedecms 织梦微信公众平台插件

 

dedecms 织梦微信公众平台插件jquery17107641620920681423="76" loaded="true" original="http://www.111com.net/notfound.jpg" />

dedecms 织梦微信公众平台插件

 

dedecms 织梦微信公众平台插件

dedecms 织梦微信公众平台插件

 

dedecms 织梦微信公众平台插件

dedecms 织梦微信公众平台插件

 

热门栏目