老规矩先添加一个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功能。