Home  >  Article  >  WeChat Applet  >  Example of how WeChat creates a custom directory

Example of how WeChat creates a custom directory

小云云
小云云Original
2018-03-19 13:28:092076browse

This article mainly shares with you examples of how to create a custom directory on WeChat. I hope it can help you.

//创建自定义菜单
    private function create_menu($access_token){
        //echo $access_token;exit;
        $url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;
        $post_data='{
                "button":[
                {
                "name":"PHP",
                "sub_button":[
                {
                    "type":"view",
                    "name":"ThinkPHP",
                    "url":"http://wap.phplee.com"
                    },
                    {
                        "type":"view",
                        "name":"微信",
                        "url":"http://wap.phplee.com/"
                    }
                    ]
                },
                {
                "name":"运维",
                "sub_button":[
                    {
                    "type":"view",
                    "name":"Linux",
                    "url":"http://wap.phplee.com"
                    },
                    {
                        "type":"view",
                        "name":"MySQL",
                        "url":"http://wap.phplee.com"
                    }
                    ]
                },
                {
                "name":"APP",
                "sub_button":[
                    {
                    "type":"view",
                    "name":"Android",
                    "url":"http://wap.phplee.com/"
                    },
                    {
                        "type":"view",
                        "name":"IOS",
                        "url":"http://wap.phplee.com/"
                    }]
                }]
            }';
        $header [] = "content-type: application/json; charset=UTF-8";
        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_URL, $url );
        //请求的方式是post
        curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "POST" );
        // 禁用后cURL将终止从服务端进行验证
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
        //不检查证书
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
        //发送头部字段
        curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );
        //告诉对方 自己的浏览器型号
        curl_setopt ( $ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)' );
        //即表示自动进行跳转抓取(如果URL发生了302重定向)继续抓取
        curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
        //自动重定向开启
        curl_setopt ( $ch, CURLOPT_AUTOREFERER, 1 );
        //发送的post参数
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post_data);
        //得到的结果不显示在屏幕上,作为变量结果储存
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
        $res = curl_exec ( $ch );
        //如果运行错误,返回一个错误号
        $flat = curl_errno ( $ch );
        if ($flat) {
            $data = curl_error ( $ch );
        }
        curl_close ( $ch );
        //拿到了返回结果后json格式转化为可以使用的数组格式
        $res = json_decode ( $res, true );
        if($res['errcode']==0){
            return true;
        }else{
            return false;
        }
    }

Use:

//微信测试
    public function Index()
    {
        //获取access_token
        if(S('access_token')){
            $access_token=S('access_token');
        }else{
            $access_token=$this->get_access_token();
            S('access_token',$access_token);
        }
        //echo $access_token;exit;
        //创建菜单
        $result=$this->create_menu($access_token);
        if($result===false){
            echo '创建菜单失败';exit;
        }else{
            echo '创建菜单成功';exit;
        }
    }

The above is the detailed content of Example of how WeChat creates a custom directory. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn