找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1822|回复: 0

[分享] 让dedecms增加微信公众平台插件

[复制链接]
发表于 2018-3-25 14:58:29 | 显示全部楼层 |阅读模式 来自 河南省新乡市
放到dede程序的plus目录下即可.
接口配置信息 按下面说明填写,
URL http://你的域名/plus/weixin.php
Token weixin (和文件中保持一致)
代码如下
代码如下       
  1. <?php

  2. define("TOKEN", "weixin");
  3. require_once(dirname(__FILE__)."/..//common.inc.php");  
  4. $dsql = new DedeSql(false);
  5. $wechatObj = new wechatCallback();
  6. $wechatObj->valid();

  7. class wechatCallback
  8. {
  9.     private $items = '';
  10.     private $articleCount = 0;
  11.     private $key = '';

  12. public function valid()
  13.     {
  14.         $echoStr = $_GET["echostr"];

  15.         //valid signature , option
  16.         if($this->checkSignature()){
  17.          echo $echoStr;
  18.             $this->responseMsg();

  19.          exit;
  20.         }
  21.     }

  22.     public function responseMsg()
  23.     {
  24.   //get post data, May be due to the different environments
  25.   $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

  26.        //extract post data
  27.   if (!empty($postStr)){

  28.                $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  29.                 $fromUsername = $postObj->FromUserName;
  30.                 $toUsername = $postObj->ToUserName;
  31.                 $this->keyword = strtolower(trim(("UTF-8","gb2312",$postObj->Content)));

  32.                 $time = time();
  33.                 $textTpl = "<xml>
  34.        <ToUserName><![CDATA[".$fromUsername."]]></ToUserName>
  35.        <FromUserName><![CDATA[".$toUsername."]]></FromUserName>
  36.        <CreateTime>".$time."</CreateTime>
  37.        <MsgType><![CDATA[text]]></MsgType>
  38.        <Content><![CDATA[%s]]></Content>
  39.        <FuncFlag>0</FuncFlag>
  40.        </xml>";   
  41.                 $picTpl = "<xml>
  42.                              <ToUserName><![CDATA[".$fromUsername."]]></ToUserName>
  43.                              <FromUserName><![CDATA[".$toUsername."]]></FromUserName>
  44.                              <CreateTime>".$time."</CreateTime>
  45.                              <MsgType><![CDATA[news]]></MsgType>
  46.                              <Content><![CDATA[]]></Content>
  47.                              <ArticleCount>%d</ArticleCount>
  48.                              <Articles>
  49.                              %s
  50.                              </Articles>
  51.                              <FuncFlag>1</FuncFlag>
  52.                             </xml>";
  53.                 if($this->keyword == 'hi' || $this->keyword == '您好'  || $this->keyword == '你好' ||$this->keyword == 'hello2bizuser' ){
  54.                  $contentStr = "输入关键字开始搜索!";//自定义欢迎回复;
  55.                  echo sprintf($textTpl, $contentStr);
  56.                 }else if( !empty( $this->keyword )){
  57.                     $this->search();
  58.                     if($this->articleCount == 0){
  59.                         $contentStr = "抱歉,没有找到与【{$this->keyword}】相关的文章,要不你更换一下关键字,可能就有结果了哦 :-) ";
  60.                         echo sprintf($textTpl, $contentStr);
  61.                     }else{
  62.                         echo sprintf($picTpl,$this->articleCount,$this->items);
  63.                     }
  64.                 }

  65.         }else {
  66.          echo "";
  67.          exit;
  68.         }
  69.     }

  70.     private function search(){
  71.         global $dsql;
  72.         $weixin_posts = $dsql->SetQuery("Select * From `dede_archives` where title like '%".$this->keyword."%' order by id desc LIMIT 0, 5");
  73.         $items = '';
  74.   $dsql->Execute();

  75.   while($weixin_post=$dsql->GetObject()){      
  76.             $title =$weixin_post->title;
  77.             $excerpt = $weixin_post->description ;//获取摘要
  78.             $thumb = $weixin_post->litpic ;//获取;
  79.             $link = '/plus/view.php?aid='.$weixin_post->id;
  80.             $items = $items . $this->get_item($title, $excerpt, $thumb, $link);  
  81.    $this->articleCount++;   
  82.   }
  83.         if($this->articleCount > 5) $this->articleCount = 5;

  84.         $this->items = $items;
  85.     }

  86.     private function get_item($title, $description, $picUrl, $url){
  87.         if(!$description) $description = $title;

  88.         return
  89.         '
  90.         <item>
  91.             <Title><![CDATA['.$title.']]></Title>
  92.             <Discription><![CDATA['.$description.']]></Discription>
  93.             <PicUrl><![CDATA[http://'.$_SERVER['HTTP_HOST'].$picUrl.']]></PicUrl>
  94.             <Url><![CDATA[http://'.$_SERVER['HTTP_HOST'].$url.']]></Url>
  95.         </item>
  96.         ';
  97.     }

  98.     private function checkSignature()
  99.     {
  100.         $signature = $_GET["signature"];
  101.         $times**p = $_GET["times**p"];
  102.         $nonce = $_GET["nonce"];   

  103.         $token = TOKEN;
  104.         $tmpArr = array($token, $times**p, $nonce);
  105.         sort($tmpArr);
  106.         $tmpStr = implode( $tmpArr );
  107.         $tmpStr = sha1( $tmpStr );

  108.         if( $tmpStr == $signature ){
  109.             return true;
  110.         }else{
  111.             return false;
  112.         }
  113.     }
  114. }
复制代码

发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;

如何回报帮助你解决问题的坛友,好办法就是点击帖子下方的评分按钮给对方加【金币】不会扣除自己的积分,做一个热心并受欢迎的人!

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则 需要先绑定手机号

关闭

站长推荐上一条 /1 下一条

QQ|侵权投诉|广告报价|手机版|小黑屋|西部数码代理|飘仙建站论坛 ( 豫ICP备2022021143号-1 )

GMT+8, 2024-3-28 21:50 , Processed in 0.043943 second(s), 9 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表