zoukankan      html  css  js  c++  java
  • 微信 之公众号自动回复查询产品

    我这里是接微擎自定义接口回复

    一、效果图

     二、代码

    /**
     * Created by PhpStorm.
     * User: Mr.Yang
     * QQ: 2575404985
     */
    
    require_once '../../../common.inc.php'; //连数据库
    include_once 'weixin.class.php'; 
    
    $wx = new Weixin();
    $wx->getMsg();
    switch ($wx->msgType){
        case 'text':
            $sell = array();
            $result = $db->query("select * from [产品表] where status = 3 and title like '%{$wx->msg['Content']}%' order by itemid desc limit 10");
            $html = "";
            while ($r = $db->fetch_array($result)) {
                $html .= "<a href='你的链接'>".$r['title']."</a>
    
    ";
            }
            $wx->reply($wx->makeText(trim($html, '
    
    ')));
            break;
    }

    weixin.class.php

    class Weixin
    {
        private $token = "vEeApDmLdwX7UnX7GL";
        public $debug = true;
        public $setFlag = false;
        public $msgType = 'text'; //('text','image','location')
        public $msg = null;
    
        public function __construct()
        {
    
        }
    
        public function getMsg()
        {
            $postStr = !empty($GLOBALS["HTTP_RAW_POST_DATA"]) ? $GLOBALS["HTTP_RAW_POST_DATA"] : file_get_contents("php://input");
            if ($this->debug) {
                $this->write_log($postStr);
            }
            if (!empty($postStr)) {
                $this->msg = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $this->msgtype = strtolower($this->msg['MsgType']);
            }
        }
    
        public function makeText($text = '')
        {
            $CreateTime = time();
            $textTpl = "<xml>
    <ToUserName><![CDATA[{$this->msg['FromUserName']}]]></ToUserName>
    <FromUserName><![CDATA[{$this->msg['ToUserName']}]]></FromUserName>
    <CreateTime>{$CreateTime}</CreateTime>
    <MsgType><![CDATA[text]]></MsgType>
    <Content><![CDATA[%s]]></Content>
    </xml>";
            return sprintf($textTpl, $text);
        }
    
    
        /**
         * 验证服务器
         */
        public function valid()
        {
            if ($this->checkSignature()) {
                if ($_SERVER['REQUEST_METHOD'] == 'GET') {
                    echo $_GET['echostr'];
                    exit;
                }
            } else {
                $this->write_log('认证失败');
                exit;
            }
        }
    
        private function checkSignature()
        {
            $signature = $_GET["signature"];
            $timestamp = $_GET["timestamp"];
            $nonce = $_GET["nonce"];
    
            $tmpArr = array($this->token, $timestamp, $nonce);
            sort($tmpArr);
            $tmpStr = implode($tmpArr);
            $tmpStr = sha1($tmpStr);
    
            if ($tmpStr == $signature) {
                return true;
            } else {
                return false;
            }
        }
    
        /**
         * 日志处理
         * @param $log
         */
        public function write_log($log)
        {
            $file = fopen("./weixin.log", "a");
            fwrite($file, $log . "
    ");
            fclose($file);
        }
    
        /**
         * 回复消息
         * @param $data
         */
        public function reply($data)
        {
            if ($this->debug) {
                $this->write_log($data);
            }
            echo $data;
        }
    }
    View Code
  • 相关阅读:
    bzoj3280
    bzoj3876
    洛谷 p1625
    bzoj1407
    bzoj1227
    bzoj1477 && exgcd学习笔记
    bzoj1345
    c#程序的config文件问题
    思维角度的重要性
    python异步初步窥探
  • 原文地址:https://www.cnblogs.com/yang-2018/p/14142360.html
Copyright © 2011-2022 走看看