PHP订阅号(认证)微信自定义菜单开发

必备网址

官方文档: https://developers.weixin.qq.com/doc/offiaccount/Custom_Menus/Creating_Custom-Defined_Menu.html

官方接口调试工具: https://mp.weixin.qq.com/debug?token=140733899&lang=zh_CN

官方测试号:https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index

故障码参考: https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Global_Return_Code.html

开发流程(以自定义菜单栏为例)
分清开发的号的权限

我是订阅号(未认证),因此不能开发自定义菜单,白白浪费时间

https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Explanation_of_interface_privileges.html

自定义菜单代码

自己不能用,就开放出来吧,我也是抄的。

<?php


/**
 * 插件名称: 微信公众号自定义菜单
 * 插件描述: 微信公众号自定义菜单
 * 作者:赤域吧
 * 插件官网: https://chiyuba.com
 */
namespace app\common\lib;
namespace addons\wxAutoReply\controller;


class  WeChatMenu
{
    public $APPID = "xx"; // 替换此处
    public $APPSECRET = "xx"; // 替换此处
    
    private function getCurl($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
    
    //请求接口方法  
    private function postcurl($url, $data = null)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)) {
            curl_setopt($ch, CURLOPT_POST, TRUE);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        return  $output = json_decode($output, true);
    }
    
    //获取access_token  
    public function index()
    {
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->APPID . "&secret=" . $this->APPSECRET;
        $date = $this->postcurl($url);
        $access_token = $date['access_token'];
        return $access_token;
    }
    //拼接参数,带着access_token请求创建菜单的接口  
    public function createMenu()
    {
        header("content-type:text/html;charset=utf-8");
        $data = array(
            'button' => array(
                array(
                    'name' => urlencode('文章'),
                    'sub_button'  => array(
                        array(
                            'type' => 'click',
                            'name' => urlencode('电影'),
                            'key'  => 'item1_1'
                        ),
                        array(
                            'type' => 'click',
                            'name' => urlencode('小说'),
                            'key'  => 'item1_2'
                        ),
                        array(
                            'type' => 'click',
                            'name' => urlencode('工具'),
                            'key'  => 'item1_3'
                        )
                    )
                ),
                array(
                    'name' => urlencode('黑科技'),
                    'sub_button'  => array(
                        array(
                            'type' => 'miniprogram',
                            'name' => urlencode('赤域吧'),
                            'url' => urlencode('http://mp.weixin.qq.com'),
                            'appid' => urlencode('wx131ed77e55c0e850'),
                            'pagepath' => urlencode('pages/index/index')
                        ),
                        array(
                            "type" => "miniprogram",
                            "name" => urlencode("聊天有术"),
                            "url" => urlencode("http://mp.weixin.qq.com"),
                            "appid" => urlencode("wx9d909157711bbb4d"),
                            "pagepath" => "pages/home/home"
                        )
                    )
                ),
                array(
                    'name' => urlencode('合作'),
                    'sub_button'  => array(
                        array(
                            'type' => 'click',
                            'name' => urlencode('官方群'),
                            'key'  => 'item2_1'
                        ),
                        array(
                            'type' => 'view',
                            'name' => urlencode('官方微信'),
                            'url'  => urlencode('https://chiyuba.com')
                        ),
                        array(
                            'type' => 'view',
                            'name' => urlencode('官方网址'),
                            'url'  => urlencode('https://chiyuba.com')
                        )
                    )
                )
            )
        );
        
        $access_token = $this->index();
         var_dump($access_token);
        $data_json = urldecode(json_encode($data));
        $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $access_token;
        // $result = $this->postcurl($url, $data_json);
       
        // echo $result;
    }
}

调试方式

方式一

http://coolaf.com/

方式二

直接在浏览器中访问。最好先用测试号,测试通过后,再使用。

版权声明:
作者:小何
链接:https://ligo100.cn/houduanjishu/php/359.html
来源:小何博客
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
< <上一篇
下一篇>>
文章目录
关闭
目 录