Home  >  Article  >  Backend Development  >  How to use PHP to develop custom menu functions for public accounts

How to use PHP to develop custom menu functions for public accounts

王林
王林Original
2023-09-21 08:25:111496browse

How to use PHP to develop custom menu functions for public accounts

How to use PHP to develop the custom menu function of public accounts requires specific code examples

With the development of social media, public accounts have become the key to interaction between enterprises and users. One of the important platforms. Among them, the custom menu function is an important part of the official account, which can help users quickly access various services and functions. This article will introduce how to use PHP to develop the custom menu function of the official account and provide specific code examples.

First, we need to obtain the access_token of the official account. This is an important credential for calling the WeChat public platform interface. The access_token can be obtained through the following code:

<?php
$appId = 'your_app_id'; // 公众号的AppId
$appSecret = 'your_app_secret'; // 公众号的AppSecret

$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appId&secret=$appSecret";
$result = file_get_contents($url);
$data = json_decode($result, true);

$accessToken = $data['access_token'];
?>

Next, we need to create a menu through a custom menu interface. This can be achieved through the following sample code:

<?php
// 菜单内容
$menuData = '{
  "button":[
    {
      "name":"菜单1",
      "sub_button":[
        {
          "type":"click",
          "name":"子菜单1",
          "key":"click_button_1"
        },
        {
          "type":"view",
          "name":"子菜单2",
          "url":"https://www.example.com"
        }
      ]
    },
    {
      "name":"菜单2",
      "sub_button":[
        {
          "type":"view",
          "name":"子菜单3",
          "url":"https://www.example.com"
        },
        {
          "type":"view",
          "name":"子菜单4",
          "url":"https://www.example.com"
        }
      ]
    },
    {
      "type":"view",
      "name":"菜单3",
      "url":"https://www.example.com"
    }
  ]
}';

// 创建菜单
$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=$accessToken";
$result = httpPost($url, $menuData);
$data = json_decode($result, true);
if ($data['errcode'] == 0) {
  echo '创建菜单成功';
} else {
  echo '创建菜单失败:' . $data['errmsg'];
}

// HTTP POST请求函数
function httpPost($url, $data)
{
  $ch = curl_init();

  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

  $output = curl_exec($ch);
  curl_close($ch);

  return $output;
}
?>

In the above code, we use CURL to send HTTP POST requests. You can use other methods to send requests according to your own needs.

Finally, we can use the custom menu interface to query the configuration of the menu. This can be achieved through the following sample code:

<?php
$url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=$accessToken";
$result = file_get_contents($url);
$menuData = json_decode($result, true);

// 打印菜单内容
print_r($menuData);
?>

Through the above code, we can obtain the configuration information of the official account menu and perform corresponding processing and adjustments.

Summary:
This article introduces how to use PHP to develop the custom menu function of the official account and provides specific code examples. Through the above code, we can obtain the access_token, create a custom menu, and query the configuration of the menu. I hope this article will be helpful to readers who want to develop custom menu functions for public accounts.

The above is the detailed content of How to use PHP to develop custom menu functions for public accounts. 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