100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 微信公众号开发之创建菜单栏代码示例(php)

微信公众号开发之创建菜单栏代码示例(php)

时间:2022-09-04 12:38:39

相关推荐

微信公众号开发之创建菜单栏代码示例(php)

思路很简单:就是先获取access_token,然后带着一定规则的json数据参数请求创建菜单的接口。废话不多讲,直接上代码。

class Wechat {public $APPID="wx******596";public $APPSECRET="ad******0"; //获取access_token public function index() { $url="https://api./cgi-bin/token?grant_type=client_credential&appid=".$this->APPID."&secret=".$this->APPSECRET; $date=postcurl($url); $access_token=$date['access_token']; return $access_token; } //拼接参数,带着access_token请求创建菜单的接口 public function createmenu(){ $data='{ "button":[ {"type":"view", "name":"精选课程", "url":"/s/ASOsHnk" }, { "name":"优研优选", "sub_button":[ {"type":"click", "name":"院校&导师", "key":"SCHOOCL_TEACHER" }, { "type":"view", "name":"快速登录", "url":"/index.php" }, { "type":"view", "name":"导师计划", "url":"/index.php/Home/About/xsjh.html" }] }, { "name":"我的", "sub_button":[ {"type":"click", "name":"联系我们", "key":"CONTACTUS" }, { "type":"view", "name":"正版软件", "url":"/model2_37685.html" }, { "type":"view", "name":"四六级冲刺", "url":"/" }] } ] }';$access_token=$this->index(); $url="https://api./cgi-bin/menu/create?access_token=".$access_token; $result=postcurl($url,$data); var_dump($result); }

备注:postcurl方法是提前写好的php请求接口的方法。代码如下:

//请求接口方法 function postcurl($url,$data = null){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)){ curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); return $output=json_decode($output,true); }

public function getCurl($url){$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);return $output;}

转:/u013077250/article/details/79041303

自己也有写:/fps2tao/openweixin

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。