Home  >  Article  >  Backend Development  >  PHP implements creating WeChat custom menu instance

PHP implements creating WeChat custom menu instance

零下一度
零下一度Original
2018-05-22 16:31:572192browse

WeChat Public PlatformCustom Menu PHP development, how is the WeChat Public Platform custom menu implemented? In fact, it is very simple. First, upgrade to a service account on the WeChat public platform, obtain the appid and appsecret, then obtain the access_token based on these two parameters, and then post a string of characters to the WeChat server based on the access_token.

Before using the general interface, you need to do the following two-step work:

1. Have a WeChat public account and obtain appid and appsecret (apply for internal testing qualifications on the public platform, and review can be obtained after passing)

2. Obtained through the certificate acquisition interfaceaccess_token

Note:

access_token is a ticket for third-party access to api resources;

access_token corresponds to the public account and is globally unique If you obtain the ticket repeatedly, the access_token obtained last time will become invalid.

Visit the following address (note to replace your appid and secret):

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, &#39;Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)&#39;);
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 = &#39;{
   "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"
      }]
    }]
}&#39;;
echo createMenu($data);
//echo getMenu();
//echo deleteMenu();

The above is the detailed content of PHP implements creating WeChat custom menu instance. For more information, please follow other related articles on the PHP Chinese website!

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