Home > Article > Backend Development > How to create WeChat custom menu with PHP
This article mainly introduces the method of creating a WeChat custom menu in PHP. It analyzes the principles, steps and specific implementation techniques of creating a WeChat custom menu in PHP with examples. Friends in need can refer to it. I hope it can help everyone. .
Before using the common interface, you need to do the following two-step work:
1. Have a WeChat public account and obtain the appid and appsecret (apply for internal beta qualification on the public platform and obtain it after passing the review)
2. Obtain access_token
through the certificate acquisition interface Note:
access_token is a ticket for third-party access to api resources;
access_token corresponds to the official account and is a globally unique ticket. Repeated acquisition will result in the last access_token obtained. Invalid.
Visit the following address (note to replace your appid and secret):
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid =APPID&secret=APPSECRET
Then you can see the return information in the browser:
{"access_token":"这里就是你的access_token","expires_in":7200}
Create a custom menu:
<?php header("Content-type: text/html; charset=utf-8"); define("ACCESS_TOKEN", "这里填入你上面获取到的access_token"); //创建菜单 function createMenu($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); $tmpInfo = curl_exec($ch); if (curl_errno($ch)) { return curl_error($ch); } curl_close($ch); return $tmpInfo; } //获取菜单 function getMenu(){ return file_get_contents("https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".ACCESS_TOKEN); } //删除菜单 function deleteMenu(){ return file_get_contents("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".ACCESS_TOKEN); } $data = '{ "button":[ { "type":"click", "name":"首页", "key":"home" }, { "type":"click", "name":"简介", "key":"introduct" }, { "name":"菜单", "sub_button":[ { "type":"click", "name":"hello word", "key":"V1001_HELLO_WORLD" }, { "type":"click", "name":"赞一下我们", "key":"V1001_GOOD" }] }] }'; echo createMenu($data); //echo getMenu(); //echo deleteMenu();
Related recommendations:
PHP code to implement the custom menu interface in the WeChat public account enterprise account
PHP implementation of creating WeChat custom menu instance
The above is the detailed content of How to create WeChat custom menu with PHP. For more information, please follow other related articles on the PHP Chinese website!