【首发】赤兔cms(ctcms)微信公众号接口插件发布

原创
原创精品        2018-12-31 13:52   来源:网络   作者:匿名 技术&&资源群:222833571|最全影视站
分享ctcms对接微信公众号的php插件,稍微修改一下数据库参数,即可适用于任意cms对接微信公众号。 ?php/** * wechat php test *///define your token//weixinabc是一个token,是一个令牌define("TOKEN", "009
 分享ctcms对接微信公众号的php插件,稍微修改一下数据库参数,即可适用于任意cms对接微信公众号。ctcms技术群:222833571。
<?php
/**
  * wechat php test
  */

//define your token
//weixinabc是一个token,是一个令牌
define("TOKEN", "009dm.com");
$wechatObj = new wechatCallbackapiTest();
error_reporting(E_ALL || ~E_NOTICE);
$wechatObj->valid();
$wechatObj->responseMsg();
//require_once(dirname(__FILE__)."./common.inc.php");
									
mysql_close($conn);
//exit;

class wechatCallbackapiTest
{
	    private $items = ''; 
    //private $articleCount = 0; 
    private $keyword = ''; 
     
	public function valid()
    {
        $echoStr = $_GET["echostr"];


        if($this->checkSignature()){
        	echo $echoStr;
			$this->responseMsg();
        	exit;
        }
    }

	private function gbk2utf8a($str){
	   return iconv('GB2312', 'UTF-8', $str);
	}
	
    public function responseMsg()
    {

		$postStr = $GLOBALS["HTTP_RAW_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))); 
     $msgType = $postObj->MsgType;//消息类型
	 $event = $postObj->Event;//时间类型,subscribe(订阅)、unsubscribe(取消订阅)
                $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>"; 
				switch($postObj->MsgType)
				{
					case 'event':

						if($event == 'subscribe')
						{
						//关注后的回复
$contentStr = "欢迎关注009动漫影视
本公众号提供在线影视观看,【港台综艺*夜问打权*新闻深喉咙】,持续关注,福利多多。输入电影或电视剧名如:“斗破苍穹”即可自动获取观看地址。如果无法播放可以点击切换线路或者留言告诉我们,我们将会在第一时间更新。";


							echo sprintf($textTpl, $contentStr);

						}
						break;
				case 'text':
				
                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->gbk2utf8a($this->keyword)."】相关的视频,要不你更换一下关键字,可能就有结果了哦 :-) "; 
                        echo sprintf($textTpl, $contentStr); 
                    }else{ 
                        echo sprintf($picTpl,$this->articleCount,$this->items); 
                    } 
                } 
				break;
					default:
						break;
				}
     
        }else { 
            echo "你好!欢迎进入微信公众号009动漫"; 
            exit; 
        } 
    } 
	
	 private function search(){ 
	 global $sql; 
	 $items = '';
	 $mysql_server_name="009dm.com"; //数据库服务器名称
    $mysql_username="009dm.com"; // 连接数据库用户名
    $mysql_password="009dm.com"; // 连接数据库密码
	$conn=mysql_connect($mysql_server_name, $mysql_username,$mysql_password);
	mysql_query('set names utf8');//用于防止插入中文数据时乱码
	$sql = "SELECT * FROM `ct_vod` WHERE `name` like '%". $this->gbk2utf8a($this->keyword)."%' order by id desc LIMIT 0 , 5;";
	mysql_select_db("ctcms");
	 if (!mysql_query($sql,$conn))
 {
   die('Error: ' . mysql_error());
 }

	$resl=mysql_query($sql);
while( $row = mysql_fetch_array($resl) ){
	@$title =$row['name'];
	@$excerpt=$row['text'];
	$thumb = $row['pic'] ;//获取缩略图; 
            $link = "/show/".$row[id].".html";//链接地址
            $items = $items . $this->get_item($title, $excerpt, $thumb, $link);    
            $this->articleCount++;
}
/* 									$sql = "SELECT * FROM `ctt_vod` WHERE `name` like '%". $this->gbk2utf8a($this->keyword)."%' order by id desc LIMIT 0 , 5;";
$items = '';
									$result = mysql_query($sql);
								//while($row = mysql_fetch_array($result))
								while($weixin_post=mysql_fetch_array($result)){         
            $title =$weixin_post->name; 
            $excerpt = $weixin_post->text ;//获取摘要 
            $thumb = $weixin_post->pic ;//获取缩略图; 
            $link = 'http://www.009dm.com/show/'.$weixin_post->id.'.html';
            $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['.$picUrl.']]></PicUrl> 
            <Url><![CDATA[http://'.$_SERVER['HTTP_HOST'].$url.']]></Url> 
        </item> 
        '; 
    } 
		
	private function checkSignature()
	{
        // you must define TOKEN by yourself
        if (!defined("TOKEN")) {
            throw new Exception('TOKEN is not defined!');
        }
        
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        		
		$token = TOKEN;
		$tmpArr = array($token, $timestamp, $nonce);
        // use SORT_STRING rule
		sort($tmpArr, SORT_STRING);
		$tmpStr = implode( $tmpArr );
		$tmpStr = sha1( $tmpStr );
		
		if( $tmpStr == $signature ){
			return true;
		}else{
			return false;
		}
	}
}

?>