【微信开发学习笔记】01消息自动回复&关键词自动回复

消息自动回复

成都创新互联公司基于成都重庆香港及美国等地区分布式IDC机房数据中心构建的电信大带宽,联通大带宽,移动大带宽,多线BGP大带宽租用,是为众多客户提供专业服务器托管报价,主机托管价格性价比高,为金融证券行业成都温江机房,ai人工智能服务器托管提供bgp线路100M独享,G口带宽及机柜租用的专业成都idc公司。

   添加第10行代码,作用是调用第22行的responseMsg回复信息方法

valid();
//调用回复方法
$wechatObj -> responseMsg();
class wechatCallbackapiTest
{
    public function valid()
    {
        $echoStr = $_GET["echostr"];
        //valid signature , option
        if($this->checkSignature()){
            echo $echoStr;
            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;
                $keyword = trim($postObj->Content);
                $time = time();
                $textTpl = "
                            
                            
                            %s
                            
                            
                            0
                            ";      
                if(!empty( $keyword ))
                {
                    $msgType = "text";
                    $contentStr = "Hello,欢迎关注";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                    echo $resultStr;
                }else{
                    echo "Input something...";
                }
        }else {
            echo "";
            exit;
        }
    }
                                                                                                                    
    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;
        }
    }
}
?>

第46行代码是自动回复的内容

$contentStr = "Hello,欢迎关注";


关键词自动回复

用if语句进行判断,if语句格式为:if($keyword== "手机"){}

if (!empty($postStr)){
                                                             
                $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $keyword = trim($postObj->Content);
                $time = time();
                $textTpl = "
                            
                            
                            %s
                            
                            
                            0
                            ";       
                if(!empty( $keyword ))
                {
                    $msgType = "text";
                    $contentStr = "Hello";
                    if($keyword == "手机") //关键词是手机时回复内容
                    {
                        $contentStr = "你回复的关键词是手机";
                    }
                    if($keyword == "电脑") //关键词是手机时回复内容
                    {
                        $contentStr = "你回复的关键词是电脑";
                    }
                    if($keyword == "相机") //关键词是手机时回复内容
                    {
                        $contentStr = "你回复的关键词是相机";
                    }
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                    echo $resultStr;
                }else{
                    echo "Input something...";
                }
        }else {
            echo "";
            exit;
        }

关键词为“手机”时回复内容

$contentStr= "你回复的关键词是手机";


标题名称:【微信开发学习笔记】01消息自动回复&关键词自动回复
URL网址:http://lszwz.com/article/pecgch.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款