微信公众号开发案例

<?php
/**
 * Created by PhpStorm.
 * User: user
 * Date: 2018/4/17
 * Time: 8:19
 */

namespace app\m\controller;
class WxDeploy extends Base
{
    /**
     *配置服务器
     *param timestamp 时间戳
     *param nonce 随机数
     *param signature 微信加密签名
     *param echostr 随机字符串
     */
    public function  deploy_verify()
    {
        $token      = "xuwei";
        $timestamp  = !empty($_GET['timestamp'])    ? $_GET['timestamp']    : false;
        $nonce      = !empty($_GET['nonce'])        ? $_GET['nonce']        : false;
        $signature  = !empty($_GET['signature'])    ? $_GET['signature']    : false;
        $echostr    = !empty($_GET['echostr'])      ? $_GET['echostr']      : false;
        $array      = array($token, $timestamp, $nonce);

        sort($array);
        $str = implode('', $array);
        $str = sha1($str);

        if($signature == $str && $echostr){
            echo $echostr;  exit;
        } else {

            // 获取微信推送过来的post数据 (xml格式)
            $postArr = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents("php://input");
            // 转化成对象
            $postObj = simplexml_load_string($postArr);

            $ToUserName     = $postObj->FromUserName;
            $FromUserName   = $postObj->ToUserName;
            $time           = time();
            $xml            = '';
            if(strtolower($postObj->MsgType) == 'text'){
                switch (strtolower($postObj->Content)){
                    case 'eat':
                        $MsgType  = "text";
                        $Content  = $this->getWhatForToday();
                        $xml = " <xml>
                             <ToUserName><![CDATA[{$ToUserName}]]></ToUserName>
                             <FromUserName><![CDATA[{$FromUserName}]]></FromUserName>
                             <CreateTime>{$time}</CreateTime>
                             <MsgType><![CDATA[{$MsgType}]]></MsgType>
                             <Content><![CDATA[{$Content}]]></Content>
                             </xml>";
                        echo $xml;
                        break;

                    case 'like':
                        $MsgType  = "text";
                        $Content  = "I like you,but just like you";
                        $xml = " <xml>
                             <ToUserName><![CDATA[{$ToUserName}]]></ToUserName>
                             <FromUserName><![CDATA[{$FromUserName}]]></FromUserName>
                             <CreateTime>{$time}</CreateTime>
                             <MsgType><![CDATA[{$MsgType}]]></MsgType>
                             <Content><![CDATA[{$Content}]]></Content>
                             </xml>";
                        echo $xml;
                        break;

                    case 'haha':
                        $MsgType  = "text";
                        $Content  = "oh , shit";
                        $xml = " <xml>
                             <ToUserName><![CDATA[{$ToUserName}]]></ToUserName>
                             <FromUserName><![CDATA[{$FromUserName}]]></FromUserName>
                             <CreateTime>{$time}</CreateTime>
                             <MsgType><![CDATA[{$MsgType}]]></MsgType>
                             <Content><![CDATA[{$Content}]]></Content>
                             </xml>";
                        echo $xml;
                        break;

                    case 'acc':
                        $MsgType  = "text";
                        $Content  =$this->get_access_token();
                        $xml = " <xml>
                             <ToUserName><![CDATA[{$ToUserName}]]></ToUserName>
                             <FromUserName><![CDATA[{$FromUserName}]]></FromUserName>
                             <CreateTime>{$time}</CreateTime>
                             <MsgType><![CDATA[{$MsgType}]]></MsgType>
                             <Content><![CDATA[{$Content}]]></Content>
                             </xml>";
                        echo $xml;
                        break;

                    case 'pic':
                        $array      = array(
                            array(
                                'title'         => 'Yellow',
                                'description'   => '温暖',
                                'picUrl'        => 'http://www.baidu.com/img/bd_logo1.png',
                                'url'           => 'http://www.baidu.com'
                            ),
                            array(
                                'title'         => 'Cc',
                                'description'   => '虫虫',
                                'picUrl'        => 'http://www.baidu.com/img/bd_logo1.png',
                                'url'           => 'http://fanyi.baidu.com'
                            ),
                            array(
                                'title'         => 'Crazy',
                                'description'   => '疯狂',
                                'picUrl'        => 'http://www.baidu.com/img/bd_logo1.png',
                                'url'           => 'http://map.baidu.com'
                            ),
                        );
                        $MsgType    = "news";
                        $Content    = "i just like you";
                        $count      = count($array);
                        $xml = "<xml>
                                <ToUserName><![CDATA[{$ToUserName}]]></ToUserName>
                                <FromUserName><![CDATA[{$FromUserName}]]></FromUserName>
                                <CreateTime>{$time}</CreateTime>
                                <MsgType><![CDATA[{$MsgType}]]></MsgType>
                                <ArticleCount>{$count}</ArticleCount>
                                <Articles>";

                        foreach($array as $value){
                            $xml .= "   <item>
                                        <Title><![CDATA[{$value['title']}]]></Title>
                                        <Description><![CDATA[{$value['description']}]]></Description>
                                        <PicUrl><![CDATA[{$value['picUrl']}]]></PicUrl>
                                        <Url><![CDATA[{$value['url']}]]></Url>
                                    </item>";
                        }

                        $xml .= "  </Articles>
                            </xml>
                            ";
                        echo $xml;
                        break;

                    default:
                        echo $xml;

                }
            }

            if(strtolower($postObj->MsgType) == 'event'){
                if(strtolower($postObj->Event) == 'click')
                {
                    switch(strtolower($postObj->EventKey)){
                        case 'get_bd':
                            $Content = "你想获取区域BD信息??";
                            $MsgType = "text";
                            $xml = " <xml>
                                     <ToUserName><![CDATA[{$ToUserName}]]></ToUserName>
                                     <FromUserName><![CDATA[{$FromUserName}]]></FromUserName>
                                     <CreateTime>{$time}</CreateTime>
                                     <MsgType><![CDATA[{$MsgType}]]></MsgType>
                                     <Content><![CDATA[{$Content}]]></Content>
                                 </xml>";
                            echo $xml;
                            break;

                        case 'get_music':
                            $Content = "Give You Some Music?";
                            $MsgType = "text";
                            $xml = " <xml>
                                     <ToUserName><![CDATA[{$ToUserName}]]></ToUserName>
                                     <FromUserName><![CDATA[{$FromUserName}]]></FromUserName>
                                     <CreateTime>{$time}</CreateTime>
                                     <MsgType><![CDATA[{$MsgType}]]></MsgType>
                                     <Content><![CDATA[{$Content}]]></Content>
                                 </xml>";
                            echo $xml;
                            break;

                        default:
                            echo $xml;

                    }
                }
            }

            echo $xml;
        }


    }

    public function Createmenu()
    {
        // 网页授权
//        $appid = $this->appid;
//        $redirect_uri = "http://m.test2.zhaojin9.com/Index/index";
//        $redirect_uri = urlencode($redirect_uri);
//        $snsapi_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri.
//            "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
        $access_token = $this->get_access_token();
        $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;
        $data = '{
               "button":[
                        {
                            "name":"威了个徐",
                            "sub_button":[
                            {
                                "type":"view",
                                "name":"微端index",
                                "url":"http://m.test.zhaojin9.com/Index/index"
                            },
                            {
                                "type":"view",
                                "name":"帅照",
                                "url":"http://m.test.zhaojin9.com/Index/img"
                            }]
                        },
                        {
                           "name":"徐了个威",
                           "sub_button":[
                           {    
                               "type":"view",
                               "name":"搜索",
                               "url":"http://www.soso.com/"
                            },
                            {
                               "type":"click",
                               "name":"赞一下我们",
                               "key":"V1001_GOOD"
                            }]
                       }]
            }';
        $res = $this->curl($url,'post',$data);
        return $res;
    }

   public  function getWhatForToday()
    {
        $menu = array(
            1  => '辣椒炒肉',
            2  => '苦瓜炒肉',
            3  => '红烧冬瓜',
            4  => '小炒豆角',
            5  => '丝瓜炒蛋',
            6  => '丝瓜炒毛豆',
            7  => '茄子炒肉',
            8  => '芹菜香干',
            9  => '黄瓜炒肉',
            10 => '胡萝卜肉丝',
            11 => '蒜苗炒肉',
            12 => '西蓝花',
            13 => '花菜',
            14 => '小炒藕丁',
            15 => '炒蘑菇',
            16 => '木耳炒肉',
            17 => '手撕包菜',
            18 => '蒜泥小白菜',
            19 => '蒜泥空心菜',
            20 => '红烧虾',
            21 => '蟹脚毛豆',
            22 => '西红柿炒蛋',
            23 => '小炒土豆片',
            24 => '洋葱炒肉',
            25 => '蒜泥菠菜',
            26 => '杏鲍菇炒肉',
            27 => '红烧鲫鱼',
            28 => '红烧鸡爪',
            29 => '红烧鸡翅',
            30 => '红烧鸭',
            31 => '凉拌菜',
            32 => '辣椒炒蛋',
            33 => '冬瓜炒肉'
        );

        $num = count($menu);

        $str = $menu[rand(1, $num)] . " " . $menu[rand(1, $num)] . " " . $menu[rand(1, $num)];

        return $str;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值