Rumah  >  Artikel  >  pembangunan bahagian belakang  >  php 怎么实现微信增加菜单

php 怎么实现微信增加菜单

藏色散人
藏色散人asal
2021-11-12 09:23:072144semak imbas

php实现微信增加菜单的方法:1、通过微信公众账号获取到appid和appsecret;2、通过获取凭证接口获取到access_token;3、通过“function createMenu($data){...}”方法创建自定义菜单即可。

php 怎么实现微信增加菜单

本文操作环境:windows7系统、PHP7.1版、DELL G3电脑

php 怎么实现微信增加菜单?

PHP实现创建微信自定义菜单的方法示例

具体如下:

在使用通用接口前,你需要做以下两步工作:

1.拥有一个微信公众账号,并获取到appid和appsecret(在公众平台申请内测资格,审核通过后可获得)

2.通过获取凭证接口获取到access_token

注意:

access_token是第三方访问api资源的票据;

access_token对应于公众号是全局唯一的票据,重复获取将导致上次获取的access_token失效。

访问下面这个地址(注意替换你的appid和secret):

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

然后在浏览器能看到一下返回信息:

{"access_token":"这里就是你的access_token","expires_in":7200}

创建自定义菜单:

<?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();

推荐学习:《PHP视频教程

Atas ialah kandungan terperinci php 怎么实现微信增加菜单. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:php能删除记录图片吗Artikel seterusnya:php正则怎么实现替换alt