The first development of WeChat Public Number, beginners climb pits

Recently, I came into contact with the development of WeChat Public Number. First of all, I climbed this pit about custo...

Recently, I came into contact with the development of WeChat Public Number. First of all, I climbed this pit about custom menu and saw the document for a while plus Baidu's summary of the implementation methods
The code above shows that I used my own access token directly, which I implemented according to my own token.The acquisition of appid and appsercet is not explained here, it will not be directly Baidu.Below is a simple way to implement the menu. You can encapsulate a lot of functions yourself. Refer to the WeChat development documentation for details.Attach your own picture at the end

<?php
//WeChat Custom Menu

header("Content-type: text/html; charset=utf-8"); //define("ACCESS_TOKEN", "5b9FZhqOXvEf2GthK76FF03kio0uUST9uqUT9L3mLli1qKdPp4dF7Ik0ZiKLGlWs4DQHMB-IveCgNvxEuDlMIrqloxrXa1IaYy5cGB3Gp9TvP_Yh-o1H-bi8Nq9BXE3JMAUbADACQK");`

function get_access_token() {

$appid = "Your WeChat Public Number appid"; $appsecret = "Your WeChat Public Number appsercet"; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); $jsoninfo = json_decode($output, true); $access_token = $jsoninfo["access_token"]; return $access_token;

}

//Create menu
function createMenu($data) {

$access_token = get_access_token(); var_dump($access_token); if (!$access_token) return; $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, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); 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;

}

//Get Menu
function getMenu() {

return file_get_contents("https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" . ACCESS_TOKEN);

}

//Delete menu
function deleteMenu() {

return file_get_contents("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" . ACCESS_TOKEN);

}

$data = '{

"button":[ { "name":"Security Dynamics", "sub_button":[ { "type":"click", "name":"One sentence a day", "key":"V1001_GOOD" }, { "type":"click", "name":"Security Notification", "key":"V1001_GOOD" }, { "type":"click", "name":"Important Notice", "key":"V1001_GOOD" }, { "type":"click", "name":"Exchange of experience", "key":"V1001_HELLO_WORLD" }, { "type":"click", "name":"Security hot spots", "key":"V1001_GOOD" }] }, { "name":"Xiao An Wei Classroom", "sub_button":[ { "type":"click", "name":"Learning Records", "key":"V1001_GOOD" }, { "type":"click", "name":"Safe Learning", "key":"V1001_GOOD" }, { "type":"click", "name":"Essential Security", "key":"V1001_HELLO_WORLD" }, { "type":"click", "name":"Team Safety", "key":"V1001_GOOD" }] }, { "name":"Test Assessment", "sub_button":[ { "type":"click", "name":"Contact Xiao An", "key":"V1001_GOOD" }, { "type":"click", "name":"Voting Activities", "key":"V1001_GOOD" }, { "type":"click", "name":"Results Query", "key":"V1001_GOOD" }, { "type":"click", "name":"Examination Evaluation", "key":"V1001_HELLO_WORLD" }, { "type":"click", "name":"User Login", "key":"V1001_GOOD" }] } ]

}';

echo createMenu($data);
?>

6 July 2020, 10:40 | Views: 10003

Add new comment

For adding a comment, please log in
or create account

0 comments