微信公众号消息回复

<?php
namespace app\index\controller;
use think\Controller;
class index extends Controller
{
    public function index(){
        //获得参数 signature nonce token timestamp echostr
        $nonce     = $_GET['nonce'];
        $token     = 'imooc';
        $timestamp = $_GET['timestamp'];
        $echostr = isset($_GET['echostr'])?$_GET['echostr']:'';
        $signature = $_GET['signature'];
        //形成数组,然后按字典序排序
        $array = array();
        $array = array($nonce, $timestamp, $token);
        sort($array);
        //拼接成字符串,sha1加密 ,然后与signature进行校验
        $str = sha1( implode( $array ) );
        if( $str  == $signature && $echostr ){
            //第一次接入weixin api接口的时候
            echo  $echostr;
            exit;
        }else{
            $this->reponseMsg();
        }
    }
    // 接收事件推送并回复
    public function reponseMsg(){
        //获取微信推送过来的数据(xml格式)
        $postArr = $GLOBALS["HTTP_RAW_POST_DATA"];
        $tmpstr  = $postArr;
        //处理消息,并设置回复类型
        $postObj = simplexml_load_string($postArr);
        //关注回复
        if (strtolower($postObj->MsgType) =="event"){
            //如果是关注事件
            if(strtolower($postObj->Event) == "subscribe"){
                //回复用户消息
                $toUser    =  $postObj->FromUserName;
                $fromUser  =  $postObj->ToUserName;
                $time      =  time();
                $msgType   =  "text";
                $content   =  "欢迎关注我的公众号:\n&nbsp;我的公众号"."\n\n微信用户的openid:\n".$postObj->FromUserName."\n\n如需关键字测试请输入:\n(  你好 || phone || composer  )\n\n图文测试请输入:\n(  单图文  ||  多图文  )";
                $template  =  " <xml>
                                  <ToUserName><![CDATA[%s]]></ToUserName>
                                  <FromUserName><![CDATA[%s]]></FromUserName>
                                  <CreateTime>%s</CreateTime>
                                  <MsgType><![CDATA[%s]]></MsgType>
                                  <Content><![CDATA[%s]]></Content>
                                  </xml> ";
                $info      =  sprintf( $template,$toUser,$fromUser,$time,$msgType,$content);
                echo  $info;
            }
        }
        //回复图文消息
        if (strtolower($postObj->MsgType) == "text" && trim($postObj->Content == "单图文")){
            $toUser    =  $postObj->FromUserName;
            $fromUser  =  $postObj->ToUserName;
            $array = array(
                array(
                    "title"=>"微博",
                    "description"=>"微博首页!",
                    "picUrl"=>"http://img.zcool.cn/community/0117e2571b8b246ac72538120dd8a4.jpg@1280w_1l_2o_100sh.jpg",
                    "url"=>"http://weibo.com/",
                ),
               array(
                    "title"=>"微博2",
                    "description"=>"微博首页2!",
                    "picUrl"=>"http://img.zcool.cn/community/0117e2571b8b246ac72538120dd8a4.jpg@1280w_1l_2o_100sh.jpg",
                    "url"=>"http://weibo.com/",
                ),
              array(
                    "title"=>"微博3",
                    "description"=>"微博首页3!",
                    "picUrl"=>"http://img.zcool.cn/community/0117e2571b8b246ac72538120dd8a4.jpg@1280w_1l_2o_100sh.jpg",
                    "url"=>"http://weibo.com/",
                ),
            );
            $template  =  "<xml>
                            <ToUserName><![CDATA[%s]]></ToUserName>
                            <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                            <MsgType><![CDATA[%s]]></MsgType>
                            <ArticleCount>".count($array)."</ArticleCount>
                            <Articles>";
            foreach ($array as $key => $v) {
                $template  .= "<item>
                                <Title><![CDATA[".$v['title']."]]></Title>
                                <Description><![CDATA[".$v['description']."]]></Description>
                                <PicUrl><![CDATA[".$v['picUrl']."]]></PicUrl>
                                <Url><![CDATA[".$v['url']."]]></Url>
                                </item>";
            }
            $template.= "</Articles></xml>";
            $time     = time();
            echo  sprintf( $template,$toUser,$fromUser,$time,"news");
        }elseif (strtolower($postObj->MsgType) == "text" && trim($postObj->Content == "多图文")) {
            $toUser    =  $postObj->FromUserName;
            $fromUser  =  $postObj->ToUserName;
            $array = array(
                array(
                    "title"=>"腾讯网",
                    "description"=>"腾讯首页!",
                    "picUrl"=>"图片url地址",
                    "url"=>"http://weibo.com/",
                ),
                array(
                    "title"=>"easywechat文档",
                    "description"=>"微信文档!",
                    "picUrl"=>"图片url地址",
                    "url"=>"http://weibo.com/",
                ),
                array(
                    "title"=>"laravel学院",
                    "description"=>"学院君!",
                    "picUrl"=>"图片url地址",
                    "url"=>"http://weibo.com/",
                ),
            );
            $template  =  "<xml>
                     <ToUserName><![CDATA[%s]]></ToUserName>
                     <FromUserName><![CDATA[%s]]></FromUserName>
                     <CreateTime>%s</CreateTime>
                     <MsgType><![CDATA[%s]]></MsgType>
                     <ArticleCount>".count($array)."</ArticleCount>
                     <Articles>";

            foreach ($array as $key => $v) {
                $template  .= "<item>
                         <Title><![CDATA[".$v['title']."]]></Title>
                         <Description><![CDATA[".$v['description']."]]></Description>
                         <PicUrl><![CDATA[".$v['picUrl']."]]></PicUrl>
                         <Url><![CDATA[".$v['url']."]]></Url>
                         </item>";
            }
            $template.= "</Articles></xml>";
            $time     = time();
            echo  sprintf( $template,$toUser,$fromUser,$time,"news");
        }else{
            switch ($postObj->Content) {
                case '你好':
                    $content  = "hello 你好!";
                    break;
                case 'phone':
                    $content  = "我的手机号码:\n12339475";
                    break;
                case 'php':
                    $content  = "我是php";
                    break;
                case 'composer':
                    $content  = "<a href='http://www.baidu.com'>composer</a>";
                    break;
                default:
                    $content = '无法匹配';
                    break;
            }
            $template = "<xml>
                     <ToUserName><![CDATA[%s]]></ToUserName>
                     <FromUserName><![CDATA[%s]]></FromUserName>
                     <CreateTime>%s</CreateTime>
                     <MsgType><![CDATA[%s]]></MsgType>
                     <Content><![CDATA[%s]]></Content>
                     </xml>";
            $toUser    =  $postObj->FromUserName;
            $fromUser  =  $postObj->ToUserName;
            $time     = time();
            $msgType  = "text";
            echo  sprintf( $template,$toUser,$fromUser,$time,$msgType,$content);
        }
    }
}
?>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值