微信公众帐号 服务号可以使用 自定义菜单功能。之前在创建菜单时一直失败,原因是$data 格式一直没有传正确,后来终于解决了。这里先记录下 顺便封装了一个类,便于自定义菜单的管理。此类仅仅是自定义菜单的管理类,并未涉及微信自定义回复和菜单事件的代码。
代码如下
/**
* @author lsh 2013-09-06
*
* 微信自定义菜单的创建|查询|取消
*/
class weixinmenu {
public static $appid = null; // 申请得到的appid
public static $secret = null; // 申请得到的secret
public static $gettoken = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
public static $createmenu = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=";
public static $selmenu ="https://api.weixin.qq.com/cgi-bin/menu/get?access_token=";
public static $delmenu = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=";
public static $opt = array(
curlopt_ssl_verifypeer => false,
curlopt_ssl_verifyhost => false,
curlopt_useragent => 'mozilla/5.0 (compatible; msie 5.01; windows nt 5.0)',
curlopt_followlocation => 1,
curlopt_autoreferer =>1,
curlopt_returntransfer => true
);
public $access_token = null;
/**
* 创建菜单
*/
public function create()
{
$this->token();
$strmeau = '{
"button":[
{
"type":"click",
"name":"菜单左",
"key":"v_menu_left"
},
{
"type":"click",
"name":"菜单中",
"key":"v_menu_center"
},
{
"type":"click",
"name":"菜单右",
"key":"v_menu_right"
}]
}';
$ret = $this->httppost(self::$createmenu.$this->access_token,self::$opt, $strmeau);
echo $ret;
}
/**
* 查询菜单
*/
public function sel()
{
$this->token();
$ret = $this->httpget(self::$selmenu.$this->access_token,self::$opt);
echo $ret;
}
/**
* 取消菜单
*/
public function del()
{
$this->token();
$ret = $this->httpget(self::$delmenu.$this->access_token,self::$opt);
echo $ret;
}
/**
* 获取token
*/
private function token()
{
$tokenurl = self::$gettoken."&appid=".self::$appid."&secret=".self::$secret;
$ret = $this->httpget($tokenurl,self::$opt);
$arrret = json_decode($ret,true);
$this->access_token = $arrret['access_token'];
}
/**
* post 模式
* @param string $url post 的地址
* @param array $opt post 选项
* @param array $post_data post 数据
* @return mixed
*/
private function httppost($url, $opt = array(),$post_data)
{
$setopt = array(
curlopt_header => 0,
curlopt_returntransfer => 1,
curlopt_url => $url,
curlopt_customrequest => 'post',
curlopt_post => 1,
curlopt_postfields => $post_data,
);
if ( !empty($opt) ) {
foreach ($opt as $key => $value) {
$setopt[$key] = $value;
}
}
$curl = curl_init($url);
foreach ($setopt as $key => $value) {
curl_setopt($curl, $key, $value );
}
$responsetext = curl_exec($curl);
curl_close($curl);
return $responsetext;
}
/**
* get 方式
* @param stinrg $url get 的url
* @param array $opt get 的选项
* @return mixed
*/
private function httpget($url, $opt = array())
{
$setopt = array(
curlopt_header => 0,
curlopt_returntransfer => 1,
curlopt_url => $url
);
if ( !empty($opt) ) {
foreach ($opt as $key => $value) {
$setopt[$key] = $value;
}
}
$curl = curl_init($url);
foreach ($setopt as $key => $value) {
curl_setopt($curl, $key, $value );
}
$responsetext = curl_exec($curl);
curl_close($curl);
return $responsetext;
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持硕编程。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!