使用公众号之前,需先要有菜单,下面来讲讲利用接口生成菜单栏。
首先获取全局access_token,这个access_token和网页授权时的access_token是两个不同的东西。
/**
* 获取全局TOKEN
* @return mixed
*/
public function getAccessToken() {
$access_token_cache = $this->getCache('access_token');
if(!$access_token_cache)
{
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.self::APPID.'&secret='.self::APPSECRET;
$result = json_decode($this->http_request($url), true);
$access_token = $result['access_token'];
$this->setCache('access_token', $access_token, $result['expires_in']);
} else {
$access_token = $access_token_cache['value'];
}
return $access_token;
}调用创建菜单的接口,生成菜单
public function index() {
$accessToken = $this->getAccessToken();
$url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$accessToken;
$res = $this->http_request($url, json_encode([
'button' => [
[
'type' => 'view',
'name' => '个人中心',
'url' => 'http://xxx.com/User/index'
]
]
], JSON_UNESCAPED_UNICODE));
var_dump($res);
}全部示例代码
<?php
require APP_PATH . '/Controller/CommonController.php';
class MenuController extends CommonController
{
// 下面三个常量来至微信公众平台配置
// 注意这里只是演示,在项目开发中请将下面的配置移到配置文件中
const TOKEN = ''; // token值自定义,必须和公众平台的设置一致
const APPID = '';
const APPSECRET = '';
public function __construct() {
if (!session_id()) {
session_start();
}
parent::__construct();
}
public function index() {
$accessToken = $this->getAccessToken();
$url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$accessToken;
$res = $this->http_request($url, json_encode([
'button' => [
[
'type' => 'view',
'name' => '个人中心',
'url' => 'http://xxx.com/User/index'
]
]
], JSON_UNESCAPED_UNICODE));
var_dump($res);
}
/**
* 获取全局TOKEN
* @return mixed
*/
public function getAccessToken() {
$access_token_cache = $this->getCache('access_token');
if(!$access_token_cache)
{
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.self::APPID.'&secret='.self::APPSECRET;
$result = json_decode($this->http_request($url), true);
$access_token = $result['access_token'];
$this->setCache('access_token', $access_token, $result['expires_in']);
} else {
$access_token = $access_token_cache['value'];
}
return $access_token;
}
/**
* 获取缓存数据
* @param $cacheKey
* @return bool
*/
private function getCache($cacheKey) {
if (isset($_SESSION[$cacheKey])) {
return ($_SESSION[$cacheKey]['expire'] > time()) ? $_SESSION[$cacheKey] : false;
} else {
return false;
}
}
/**
* 设置缓存
* @param $cacheKey
* @param $value
* @param $expire
*/
private function setCache($cacheKey, $value, $expire) {
$_SESSION[$cacheKey] = [
'value' => $value,
'expire' => time() + $expire
];
}
/**
* 发起请求
* @param $url
* @param null $data
* @return mixed
*/
private function http_request($url, $data = null) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data))
{
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data))
);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
}

fffffasdfasdfasdfasdfas;kdfjlaskl;dfjaskl;dfjakls;dfjkal;sfdjlaks;df als;dfj awkls;dfj asl;dfj aslk;dfjaskl;dfjas;ldf
访客评论