php 開發微信公衆號雜記————自定義菜單

在項目根目錄(可能還能夠放到一個可訪問的文件夾【猜想,懶於驗證】)新建wechat_menu.phpphp

代碼以下json

$appid = "kjllkjlkj456564";         //本身公衆號的APPID
$appsecret = "54ads4564adsf464f";        //本身公衆號的 secret  ,須要自行設置
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;

$output = https_request($url);
$access_token_Arr    = json_decode($output,true);
$access_token        =   $access_token_Arr['access_token'];

$jsonmenu = '{
    "button": [
        {
            "type": "view", 
            "name": "遊戲簽到", 
            "url": "http://wx.test.com/"
        },
        {
            "name": "遊戲充值", 
            "sub_button": [
                {
                    "type": "view", 
                    "name": "購買遊戲卡", 
                    "url": "http://wx.test.com/index.php/pay/index"
                }, 
                {
                    "type": "view", 
                    "name": "新版下載", 
                    "url": "http://wx.test.com/index.php/down/index"
                }
            ]
        }
    ]
}';


$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;
$result = https_request($url, $jsonmenu);
//var_dump($result);

function https_request($url,$data = null){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}

確認該項目的域名解析到公網環境後,直接訪問
http://wx.test.com/wechat_menu.phpapi

成功後返回app

{"errcode":0,"errmsg":"ok"}

其他都是錯誤,具體對照開發文檔的錯誤代碼curl

相關文章
相關標籤/搜索