微信公众号(服务号)当用户关注的时候,显示两条服务器信息

来源:互联网 发布:淘宝网太阳镜 编辑:程序博客网 时间:2024/05/21 11:53


思路 : 1,自动回复的时候 发一次 2.触发客服接口在发一次

实现代码如下:

$wechatObj = new wechatCallbackapiTest();$wechatObj->responseMsg();class wechatCallbackapiTest{       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);                $RX_TYPE = trim($postObj->MsgType);                switch($RX_TYPE)                {                    case "text":                        $resultStr = $this->handleText($postObj);                        break;                    case "event":                        $resultStr = $this->handleEvent($postObj);                        break;                    default:                        $resultStr = "Unknow msg type: ".$RX_TYPE;                        break;                }                echo $resultStr;        }else {            echo "";            exit;        }    }    public function actoken($openid){        $atoken = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx6ff2a8b&secret=be2e6ae88af27f8c3971127fc1");        $atoken = json_decode($atoken);        $atoken = $atoken->access_token;        $createUser = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$atoken;        $post_data = array("touser" => "$openid","msgtype" => "text","text"=>array("content"=>"Hello World"));                $data = json_encode($post_data);        $this->http_post_json($createUser,$data);    }    public function http_post_json($url, $jsonStr)    {        $ch = curl_init();        curl_setopt($ch, CURLOPT_POST, 1);        curl_setopt($ch, CURLOPT_URL, $url);        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        curl_setopt($ch, CURLOPT_HTTPHEADER, array(                'Content-Type: application/json; charset=utf-8',                'Content-Length: ' . strlen($jsonStr)            )        );        $response = curl_exec($ch);        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);        curl_close($ch);    }       public function handleText($postObj)    {        $fromUsername = $postObj->FromUserName;        $toUsername = $postObj->ToUserName;        $keyword = trim($postObj->Content);        $time = time();        $textTpl = "<xml>                    <ToUserName><![CDATA[%s]]></ToUserName>                    <FromUserName><![CDATA[%s]]></FromUserName>                    <CreateTime>%s</CreateTime>                    <MsgType><![CDATA[%s]]></MsgType>                    <Content><![CDATA[%s]]></Content>                    <FuncFlag>0</FuncFlag>                    </xml>";                     if(!empty( $keyword ))        {            $msgType = "text";            $contentStr = "欢迎";            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);            echo $resultStr;        }else{            echo "Input something...";        }    }    public function handleEvent($object)    {        $contentStr = "";        switch ($object->Event)        {            case "subscribe":            // 这个是关注事件m先触发客服接口,然后在输出自动回复的内容                $to = $this->actoken($object->FromUserName);                $contentStr = /*$object->FromUserName.*/'欢迎';                break;            default :                $contentStr = "Unknow Event: ".$object->Event;                break;        }        $resultStr = $this->responseText($object, $contentStr);        return $resultStr;    }        public function responseText($object, $content, $flag=0)    {        $textTpl = "<xml>                    <ToUserName><![CDATA[%s]]></ToUserName>                    <FromUserName><![CDATA[%s]]></FromUserName>                    <CreateTime>%s</CreateTime>                    <MsgType><![CDATA[text]]></MsgType>                    <Content><![CDATA[%s]]></Content>                    <FuncFlag>%d</FuncFlag>                    </xml>";        $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag);        return $resultStr;    }}

有不明白的可以直接留言

最终的效果图如下:




0 0
原创粉丝点击