search
Homephp教程PHP源码微信公众平台开发通过php创建自定义菜单

自定义菜单可以在后台设置但如果你使用了开发者之后那么后台的自定义菜单功能就失效了,必须通过程序来创建了,下面一起来看一个简单的通过php创建自定义菜单的例子。

<script>ec(2);</script>

验证配置

 代码如下 复制代码

define("TOKEN", "xxx");//改成自己的TOKEN
define('APP_ID', 'xxxx');//改成自己的APPID
define('APP_SECRET', 'xxx');//改成自己的APPSECRET

$wechatObj = new wechatCallbackapiTest();

if (isset($_GET['echostr'])) {
    $wechatObj->valid();
}else{
    $wechatObj->responseMsg();
}

class wechatCallbackapiTest{}

这个类就介绍了网上多了去了,大家可以自行搜索了下,我只介绍两个创建自定义菜单的函数

 代码如下 复制代码


  /**
     * 获取access_token
     */
    public function get_access_token()
    {
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APP_ID."&secret=".APP_SECRET;
        $data = json_decode(file_get_contents($url),true);
        if($data['access_token']){
            return $data['access_token'];
        }else{
            return "获取access_token错误";
        }
    }
 
  /**
     * 创建菜单
     * @param $access_token 已获取的ACCESS_TOKEN
     */
    public function createmenu($access_token)
    {
        $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;
        $arr = array( 
            'button' =>array(
                array(
                    'name'=>urlencode("aaaa"),
                    'sub_button'=>array(
                        array(
                            'name'=>urlencode("bbbb"),
                            'type'=>'click',
                            'key'=>'VCX_WEATHER'
                        ),
                        array(
                            'name'=>urlencode("cccc"),
                            'type'=>'click',
                            'key'=>'VCX_IDENT'
                        )
                    )
                ),
                array(
                    'name'=>urlencode("dddd"),
                    'sub_button'=>array(
                        array(
                            'name'=>urlencode("xxxx"),
                            'type'=>'click',
                            'key'=>'VCX_GUAHAPPY'
                        ),
                        array(
                            'name'=>urlencode("xxxx"),
                            'type'=>'click',
                            'key'=>'VCX_LUCKPAN'
                        )
                    )
                ),
                array(
                    'name'=>urlencode("xxxx"),
                    'sub_button'=>array(
                        array(
                            'name'=>urlencode("aaaa"),
                            'type'=>'click',
                            'key'=>'VCX_ABOUTME'
                        ),
                        array(
                            'name'=>urlencode("aaaaa"),
                            'type'=>'click',
                            'key'=>'VCX_JOBINFORMATION'
                        )
                    )
                )
            )
        );
        $jsondata = urldecode(json_encode($arr));
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_POST,1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$jsondata);
        curl_exec($ch);
        curl_close($ch);
    }
 
}

使用方法是

 代码如下 复制代码

$access_token = $wechatObj->get_access_token();//获取access_token
$wechatObj->createmenu($access_token);//创建菜单到微信公共平台

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.