微信公眾賬號服務號自定義菜單配置與實現


微信自定義菜單 實現其實非常簡單。只不過很多人不知道怎么寫代碼 怎么去做。

下面po上我的實現方法,以備后用。

1、拿到access_token

<?php
/**
 * get access token
 * @author Ren Kan <rk6556(a)gmail.com>
 */

header("Content-type: text/html; charset=utf-8");  

$appid = "YOUR APPID";
$appsecret = "YOUR APP SECRET";
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;

$content = file_get_contents($url);

$info = json_decode($content);

echo $info->access_token;

?>

2、配置自定義菜單

$ACCESS_TOKEN = "THE ACCESS TOKEN GOT ABOVE";

/* 這里是視圖類型的自定義菜單反饋條目
    {
        "type": "view",
        "name": "關於我們",
        "url": "http://blog.csdn.net/rk2900"
    }

*/


$data = '{
    "button":
    [
        {	
            "name": "綁定相關",
            "sub_button":
            [
                {
                    "type": "click",
                    "name": "綁定賬號",
                    "key": "BIND_DEVICE"
                },
                {
                    "type": "click",
                    "name": "綁定狀態",
                    "key": "BIND_INFO"
                },
                {
                    "type": "click",
                    "name": "解除綁定",
                    "key": "BIND_CANCEL"
                }
            ]
        },
        {
            "name": "相冊操作",
            "sub_button":
            [
                {
                    "type": "click",
                    "name": "刪除上一張",
                    "key": "DELETE_LAST"
                },
                {
                    "type": "click",
                    "name": "瀏覽相冊",
                    "key": "BROWSE"
                }
            ]
        },
        {
            "name": "購買微相框",
            "sub_button": 
            [
                {
                    "type": "click",
                    "name": "客服電話",
                    "key": "PHONE"
                },
                {
                    "type": "click",
                    "name": "購買方式",
                    "key": "BUY"
                }
            ]
        }
    ]
 }';
 echo $data;
 
 $ch = curl_init(); 
 curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$ACCESS_TOKEN}"); 
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
 curl_setopt($ch, CURLOPT_AUTOREFERER, 1); 
 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
 echo $ch;
 $tmpInfo = curl_exec($ch); 
 if (curl_errno($ch)) 
 {  
	echo 'Errno'.curl_error($ch);
 }
 
 curl_close($ch); 
 echo $tempInfo;  
 /**/

以上代碼構建以后,只需要終端利用命令php shellname.php依次執行即可。
效果在公眾賬號頁面過會就會看到更新了。
效果如下:


感謝 微相框公眾賬號提供支持。

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com