• 微信配置


    <?php
    include('mysql.php');
    //define your token
    define("TOKEN", "freept");
    $wechatObj = new wechatCallbackapiTest();

    if(!isset($_GET['echostr'])){
    $wechatObj->responseMsg();
    }else{
    $wechatObj->valid();
    }

    class wechatCallbackapiTest
    {
    public function valid()
    {
    $echoStr = $_GET["echostr"];

    //valid signature , option
    if($this->checkSignature())
    {
    echo $echoStr;
    exit;
    }
    }

    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;
    }
    }
        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))
    {
                    /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
                       the best way is to check the validity of xml by yourself */
                    //libxml_disable_entity_loader(true);
                  $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                    $RE_TYPE = trim($postObj->MsgType);
    //用户发送的消息类型判断
    switch($RE_TYPE)
    {
    case "event":
    $result = $this->receiveEvent($postObj);
    break;
    case "text":
    $result = $this->receiveText($postObj);
    break;
    case "image":
    $result = $this->receiveImage($postObj);
    break;
    case "voice":
    $result = $this->receiveVoice($postObj);
    break;
    case "location":
    $result = $this->receiveLocation($postObj);
    break;
    default:
    $result = "unknow msg type: ".$RX_TYPE;
                        break;
    }
    echo $result;
            }else {
    echo "";
    exit;
    }
        }
    //接收事件推送消息
    private function receiveEvent($object)
    {
    $content = "";
    switch($object->Event)
    {
    case "subscribe":
    $content = "欢迎关注春雨科技公司,互联网领头企业哦。";
    break;
    case "SCAN":
    $content = "第".$object->EventKey."期活动签到成功,祝您会议愉快!";
    //插入统计数据表
    $where = 'qiandao_user="'.$object->FromUserName.'"';
    $re = mysql_fetch_assoc(select('fpt_qiandao',$where));
    $where = 'count_user="'.$object->FromUserName.'"';
    $row = mysql_fetch_assoc(select('fpt_count',$where));
    if($re['qiandao_user']&&$row['count_user']){ //如果数据库存在该用户,就更新count
    $where = 'count_id="'.$row['count_id'].'"';
    $count = $row['count_qiandao'];
    $data['count_qiandao']= ++$count;
    update('fpt_count',$data,$where);
    }elseif($re['qiandao_user']&&!$row['count_user']){ //如果数据库不存在该用户,就插入数据
    $count['count_user'] = $object->FromUserName;
    $count['count_qiandao'] = 1;
    insert('fpt_count',$count);
    }elseif(!$re['qiandao_user']&&$row['count_user']){
    $columns['qiandao_user'] = $object->FromUserName;
    $columns['qiandao_scene'] = $object->EventKey;
    insert('fpt_qiandao',$columns);

    $where = 'count_id="'.$row['count_id'].'"';
    $count = $row['count_qiandao'];
    $data['count_qiandao']= ++$count;
    update('fpt_count',$data,$where);
    }else{
    $columns['qiandao_user'] = $object->FromUserName;
    $columns['qiandao_scene'] = $object->EventKey;
    insert('fpt_qiandao',$columns);
    if($count['count_user']){
    $where = 'count_id="'.$row['count_id'].'"';
    $count = $row['count_qiandao'];
    $data['count_qiandao']= ++$count;
    update('fpt_count',$data,$where); 
    }else{
    $count['count_user'] = $object->FromUserName;
    $count['count_qiandao'] = 1;
    insert('fpt_count',$count);
    }
    }
    break;
    default:
    break;
    }
    $result = $this->transmitText($object,$content);
    return $result;
    }
    //接收文本信息
    private function receiveText($object)
    {
    $keyword = trim($object->Content);
    //判断fpt_member表是否存在openid,如果不存在就插入openid
    $where = 'member_openid="'.$object->FromUserName.'"';
    $result = mysql_fetch_assoc(select('fpt_member',$where));
    if(!$result){
    $data['member_openid'] = $object->FromUserName;
    insert('fpt_member',$data);
    }
    //在数据库中匹配用户发送的关键字
    $where = 'act_key ="'.$keyword.'"';
    $re = mysql_fetch_assoc(select("fpt_act",$where));
    if($keyword == "文本")
    {
    //回复文本消息
    $content = "这是一个文本信息";
    $result = $this->transmitText($object,$content);
    }elseif($keyword == $re['act_key']){
    //回复图文消息
    $content = array();
    $content[] = array("Title"=>$re['act_title'],
    "Description"=>$re['act_summary'],
    "PicUrl"=>$re['act_logo'],
    "Url"=>'http://aino.chunyukj.com/html/activities.php?act_key='.$re['act_key'].'&openid='.$object->FromUserName);
    $result = $this->transmitNews($object,$content);
    }
    return $result;
    }

    //推送事件
    private function transmitEvent($object,$content){
    $eventTpl ="<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[event]]></MsgType>
    <Event><![CDATA[%s]]></Event>
    <Content><![CDATA[%s]]></Content>
    <EventKey><![CDATA[%s]]></EventKey>
    <Ticket><![CDATA[%s]]></Ticket>
    </xml>";
    $result = sprintf($eventTpl, $object->FromUserName, $object->ToUserName,time(), $content);
    return $result;
    }
    //回复文本消息
    private function transmitText($object,$content){
    $textTpl = "<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[text]]></MsgType>
    <Content><![CDATA[%s]]></Content>
    </xml>";
    $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName,time(), $content);
    return $result;
    }
    //回复图文消息
    private function transmitNews($object,$arr_item){
    if(!is_array($arr_item))return;
    $itemTpl = "<item>
    <Title><![CDATA[%s]]></Title>
    <Description><![CDATA[%s]]></Description>
    <PicUrl><![CDATA[%s]]></PicUrl>
    <Url><![CDATA[%s]]></Url>
    </item>";
    $item_str = "";
    foreach($arr_item as $item){
    $item_str .= sprintf($itemTpl,$item['Title'],$item['Description'],$item['PicUrl'],$item['Url']);
    }
    $newsTpl = "<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[news]]></MsgType>
    <Content><![CDATA[]]></Content>
    <ArticleCount>%s</ArticleCount>
    <Articles>$item_str</Articles>
    </xml>";
    $result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName,time(),count($arr_item));
    return $result;
    }
    }






  • 相关阅读:
    编译安装zabbix3.2
    编译安装PHP的参数 --with-mysql --with-mysqli --with-apxs2默认路径
    MySql中指定符号分割并分行展示
    Linux中vim编辑器常用命令
    Ubuntu中安装配置 JDK与apache
    Ubuntu中python链接本地数据库
    oracle函数笔记(1)
    Oracle的五种约束
    斐波那契数列
    python计算圆面积
  • 原文地址:https://www.cnblogs.com/muxiaoye/p/dbf58b89321296da037b7a032385d7db.html
Copyright © 2020-2023  润新知