老规矩先添加一个location:
location /menu_create {
content_by_lua_file /path/to/menu_create.lua;
}
然后就是干货:
local http = require "resty.http"
local cjson = require "cjson"
local hc = http:new()
-- menu setting
local button1 = {}
local button2 = {}
-- button1
button1["name"] = "主按钮1"
button1["sub_button"] = {}
button1["sub_button"][1] = {}
button1["sub_button"][1]["type"] = "view"
button1["sub_button"][1]["name"] = "子按钮1"
button1["sub_button"][1]["url"] = "http://www.baidu.com"
button1["sub_button"][2] = {}
button1["sub_button"][2]["type"] = "view"
button1["sub_button"][2]["name"] = "子按钮2"
button1["sub_button"][2]["url"] =
-- button2
button2["type"] = "view"
button2["name"] = "主按钮2"
button2["url"] = "http://www.baidu.com"
local menu = {}
menu["button"] = {button1, button2}
local menu_json = cjson.encode(menu)
ngx.say(menu_json)
local res, err = hc:request_uri ("https://api.weixin.qq.com/cgi-bin/menu/create", {
method = "POST",
query = {
access_token = "ACCESS_TOKEN",
},
ssl_verify = false,
body = menu_json,
})
if not res then
ngx.say("failed to request: ", err)
return
end
ngx.say(res.body)
主要是演示一下如何利用lua_resty_http实现https的POST功能。
本文介绍如何使用Lua语言配合lua_resty_http库实现通过HTTPS POST请求来创建微信公众号的自定义菜单。具体步骤包括加载http模块,构造菜单JSON数据,设置菜单按钮及子按钮的相关属性,并最终发送请求到微信API接口。
1359

被折叠的 条评论
为什么被折叠?



