微信公众号对接网站实现自动回复搜索找电影关注自动回复

Strjson
2019-04-23 / 0 评论 / 1,056 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2021年04月11日,已超过491天没有更新,若内容或图片失效,请留言反馈。
<?php
header('Content-type:text');

define("TOKEN", "qingeedy");
$wechatObj = new wechatCallbackapiTest();
if (!isset($_GET['echostr'])) {
    $wechatObj->responseMsg();
}else{
    $wechatObj->valid();
}

class wechatCallbackapiTest{
    //验证签名
    public function valid(){
        $echoStr = $_GET["echostr"];
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);
        if($tmpStr == $signature){
            echo $echoStr;
            exit;
        }
    }
    //响应消息
    public function responseMsg(){
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
         $textTpl = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType><Content><![CDATA[%s]]></Content></xml>";
        //$postStr = 'huihui';      //调试用
        if (!empty($postStr)){
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $event = $postObj->Event;
            $RX_TYPE = trim($postObj->MsgType);
            // $RX_TYPE = "text";//gnagcai  zhege  meiqudiao
            //消息类型分离
            switch ($RX_TYPE){
                case "text":
                    $result = $this->receiveText($postObj);
                    break;
                case "image":
                    $result = $this->receiveImage($postObj);
                    break;
                    case 'event':

                        if($event == 'subscribe')
                        {
                            $contentStr = "欢迎关注";
                            $result = $this->receiveText2($postObj);    

                        }
                    break;
                default:
                    $result = "unknown msg type: ".$RX_TYPE;
                    break;
            }
            echo $result;
        }else {
            echo "this i s failed";
            exit;
        }
    }

    //接收文本消息
    private function receiveText($object){
        $a = ($object->Content);
        $keyword = trim($a);//关键字
        $arr[] = array(
                "Title" =>"为您找到【".$keyword."】的所有搜索结果!",
                "Description" => "享受闲暇,不负好时光!点我查看 ☞",
                "PicUrl" => "https://app.qingee.net/weixin/img/sousou.png",
                "Url" => "http//he-ad.cn/?wd=".$keyword
            );
        $result="";
        if (is_array($arr)) {
            if (isset($arr[0])) {
                $result = $this->transmitNews($object, $arr);
            }
        } else {
            $result = $this->transmitText($object, $arr);
        }
        return $result;

    }

    //接收文本消息
    private function receiveText2($object){
        $a = ($object->Content);
        $keyword = trim($a);//关键字
        $arr="欢迎关注我们,橙子网络";
        $result="";
        if (is_array($arr)) {
            if (isset($arr[0])) {
                $result = $this->transmitNews($object, $arr);
            }
        } else {
            $result = $this->transmitText($object, $arr);
        }
        return $result;

    }

    //接收图片消息
    private function receiveImage($object){
        $content = array("MediaId"=>$object->MediaId);
        $result = $this->transmitImage($object, $content);
        return $result;
    }

    //回复文本消息
    private function transmitText($object, $content){
        if (!isset($content) || empty($content)){
            return "";
        }
        $xmlTpl =  "<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[text]]></MsgType>
                        <Content><![CDATA[%s]]></Content>
                    </xml>";
        $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), $content);

        return $result;
    }

    //回复图文消息
    private function transmitNews($object, $newsArray){
        if(!is_array($newsArray)){
            return "";
        }
        $itemTpl = "<item>
                        <Title><![CDATA[%s]]></Title>
                        <Description><![CDATA[%s]]></Description>
                        <PicUrl><![CDATA[%s]]></PicUrl>
                        <Url><![CDATA[%s]]></Url>
                    </item>";
        $item_str = "";
        foreach ($newsArray as $item){
            $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
        }
        $xmlTpl = "<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[news]]></MsgType>
                        <ArticleCount>%s</ArticleCount>
                        <Articles>$item_str</Articles>
                    </xml>";

        $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray));
        return $result;
    }

    //回复图片消息
    private function transmitImage($object, $imageArray){
        $itemTpl = "<Image>
                        <MediaId><![CDATA[%s]]></MediaId>
                    </Image>";

        $item_str = sprintf($itemTpl, $imageArray['MediaId']);

        $xmlTpl =  "<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[image]]></MsgType>
                        $item_str
                    </xml>";
        $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time());
        return $result;
    }

}
?>
0

评论 (0)

取消