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

坚持就是胜利。胜利不重要,重要的是能坚持人生最大的哀痛,是子欲孝而亲不在!人生最大的悲剧,是家未富而人先亡,人生最大的可怜,是弥留之际才明白自己是应该做什么的!
放到dede程序的plus目录下即可.

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

代码如下

 代码如下

<?php
 
define("TOKEN", "weixin");
require_once(dirname(__FILE__)."/..//common.inc.php");  
$dsql = new DedeSql(false);
$wechatObj = new wechatCallback();
$wechatObj->valid();
 
class wechatCallback
{
    private $items = '';
    private $articleCount = 0;
    private $key = '';
 
 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(("UTF-8","gb2312",$postObj->Content)));
 
                $time = time();
                $textTpl = "<xml>
       <ToUserName><![CDATA[".$fromUsername."]]></ToUserName>
       <FromUserName><![CDATA[".$toUsername."]]></FromUserName>
       <CreateTime>".$time."</CreateTime>
       <MsgType><![CDATA[text]]></MsgType>
       <Content><![CDATA[%s]]></Content>
       <FuncFlag>0</FuncFlag>
       </xml>";    
                $picTpl = "<xml>
                             <ToUserName><![CDATA[".$fromUsername."]]></ToUserName>
                             <FromUserName><![CDATA[".$toUsername."]]></FromUserName>
                             <CreateTime>".$time."</CreateTime>
                             <MsgType><![CDATA[news]]></MsgType>
                             <Content><![CDATA[]]></Content>
                             <ArticleCount>%d</ArticleCount>
                             <Articles>
                             %s
                             </Articles>
                             <FuncFlag>1</FuncFlag>
                            </xml>";
                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 `dede_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
        '
        <item>
            <Title><![CDATA['.$title.']]></Title>
            <Discription><![CDATA['.$description.']]></Discription>
            <PicUrl><![CDATA[http://'.$_SERVER['HTTP_HOST'].$picUrl.']]></PicUrl>
            <Url><![CDATA[http://'.$_SERVER['HTTP_HOST'].$url.']]></Url>
        </item>
        ';
    }
 
    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如何实现任意页面调用当前会员信息的方

DedeCMS织梦判断是否有二级栏目输出HTML代码

织梦dedecms dede:arclist输出取消换行符

dedecms文章添加字段填栏目id,内容页调用字段里的栏目文章

dedeCMS首页列表页文章页调用tag的方法