本文实例讲述了PHP实现微信公众号企业号自定义菜单接口。分享给大家供大家参考,具体如下:
creatMenu();//创建菜单
PHP;">
class Wechat
{
private function getAccessToken() //获取access_token
{
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".AppId."&secret=".AppSecret;
$data = getCurl($url);//通过自定义函数getCurl得到https的内容
$resultArr = json_decode($data,true);//转为数组
return $resultArr["access_token"];//获取access_token
}
public function creatMenu()//创建菜单
{
$accessToken = $this->getAccessToken();//获取access_token
$menuPostString = '{//构造POST给微信服务器的菜单结构体
"button":[
{
"name":"产品介绍","sub_button":[
{
"type":"view","name":"分销A型","url":"http://www.yourwebname.com/fenxiao/jianjie/soft.html"
},{
"type":"view","name":"分销B型","url":"http://www.yourwebname.com/fenxiaob/jianjie/soft.html"
},"name":"地接批发","url":"http://www.yourwebname.com/dijie/jianjie/soft.html"
},"name":"精简组团","url":"http://www.yourwebname.com/zutuan/jianjie/soft.html"
},"name":"直客网站","url":"http://www.yourwebname.com/tripal/jianjie/soft.html"
}]
},{
"name":"申请试用","sub_button":[
{
"type":"click","key":"fxa"
},{
"type":"click","key":"fxb"
},"key":"dj"
},"key":"zutuan"
},"key":"zhike"
}
]
},{
"name":"博纵在线","name":"企业介绍","url":"http://www.yourwebname.com/about.html"
},"name":"公司新闻","url":"http://www.yourwebname.com/news/company/"
},"name":"联系我们","url":"http://www.yourwebname.com/contact.html"
}
]
}
]
}';
$menuPostUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$accessToken;//POST的url
$menu = dataPost($menuPostString,$menuPostUrl);//将菜单结构体POST给微信服务器
}
}
function getCurl($url){//get https的内容
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//不输出内容
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
$result = curl_exec($ch);
curl_close ($ch);
return $result;
}
function dataPost($post_string,$url) {//POST方式提交数据
$context = array ('http' => array ('method' => "POST",'header' => "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) \r\n Accept: */*",'content' => $post_string ) );
$stream_context = stream_context_create ( $context );
$data = file_get_contents ( $url,FALSE,$stream_context );
return $data;
}
?>
更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》及《》
希望本文所述对大家PHP程序设计有所帮助。
原文链接:https://www.f2er.com/wxmp/16919.html