加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

dedecms增加微信公众号平台发布插件

发布时间:2020-12-14 17:07:43 所属栏目:资源 来源:网络整理
导读:现在微信公众号很火,基本是个网站都会再运营一个公众号,因此我们用DEDECMS建站也需要与时俱进对接进入公众号功能,在公众号发布文章,提高用户粘度 把以下代码保存为winxin.php放到dede程序的plus目录下. ? ? ? 代码如下 ?? ?php define("TOKEN","weixin")

现在微信公众号很火,基本是个网站都会再运营一个公众号,因此我们用DEDECMS建站也需要与时俱进对接进入公众号功能,在公众号发布文章,提高用户粘度

  1. 把以下代码保存为winxin.php放到dede程序的plus目录下.

?

?

?

代码如下 ??

<?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启用SSL和多站点支持HTTPS访问后缩略图无法自动生成的解决办法

下一篇:DedeCMS实现tag标签伪静态的方法

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读