微信公众号开发案例

/**
 * 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 = " 
                             {$ToUserName}]]>
                             {$FromUserName}]]>
                             {$time}
                             {$MsgType}]]>
                             {$Content}]]>
                             ";
                        echo $xml;
                        break;

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

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

                    case 'acc':
                        $MsgType  = "text";
                        $Content  =$this->get_access_token();
                        $xml = " 
                             {$ToUserName}]]>
                             {$FromUserName}]]>
                             {$time}
                             {$MsgType}]]>
                             {$Content}]]>
                             ";
                        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 = "
                                {$ToUserName}]]>
                                {$FromUserName}]]>
                                {$time}
                                {$MsgType}]]>
                                {$count}
                                ";

                        foreach($array as $value){
                            $xml .= "   
                                        <![CDATA[</span>{$value[<span style="color:#e6db74;">'title'</span>]}<span style="color:#e6db74;">]]>
                                        {$value['description']}]]>
                                        {$value['picUrl']}]]>
                                        {$value['url']}]]>
                                    ";
                        }

                        $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 = " 
                                     {$ToUserName}]]>
                                     {$FromUserName}]]>
                                     {$time}
                                     {$MsgType}]]>
                                     {$Content}]]>
                                 ";
                            echo $xml;
                            break;

                        case 'get_music':
                            $Content = "Give You Some Music?";
                            $MsgType = "text";
                            $xml = " 
                                     {$ToUserName}]]>
                                     {$FromUserName}]]>
                                     {$time}
                                     {$MsgType}]]>
                                     {$Content}]]>
                                 ";
                            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;
    }
}

你可能感兴趣的:(微信公众号)