php删除添加接口,thinkphp调用微信接口完成自定义菜单的添加和删除

参考微信开发接口:http://mp.weixin.qq.com/wiki/13/43de8269be54a0a6f64413e4dfa94f39.html

表格结构:

主菜单:

eaa7d5f9dccdbb110b6e609a29f2755c.png

二级菜单:

939b728bae7daabffb93bae46c531b0e.png

思路:通过存储的appid和appsecret获取access_token--->通过存储的菜单拼接提交的body--->通过微信的接口post提交

代码:

class WeixinAction extends Action{

//创建自定义菜单

public function create(){

$api=M('wx_weixin_inf')->find();//取出数据库中保存的appid和appsecret

$url_get='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$api['wei_appID'].'&secret='.$api['wei_appSecret'];

$json=json_decode(file_get_contents("$url_get"));

if($api['wei_appID']==false||$api['wei_appSecret']==false){

$this->error('必须先填写【AppId】【 AppSecret】');exit;}

//开始拼接数据

$data = '{"button":[';

$class=M('wx_menu1_inf')->limit(3)->order('men1_order')->select();//取出顶级菜单

$k=1;

foreach($class as $key=>$vo){

//主菜单

$data.='{"name":"'.$vo['men1_name'].'",';

//取出二级菜单

$c=M('wx_menu2_inf')->where(array('men1_id'=>$vo['men1_id']))->order('men2_order')->limit(5)->select();

$count=M('wx_menu2_inf')->where(array('men1_id'=>$vo['men1_id']))->limit(5)->count();

$num=1;

//拼接子菜单

if($c!=false){

$data.='"sub_button":[';

foreach($c as $voo){

if($num==$count) $data.='{"type":"view","name":"'.$voo['men2_name'].'","url":"'.$voo['men2_url'].'"}';

else $data.='{"type":"view","name":"'.$voo['men2_name'].'","url":"'.$voo['men2_url'].'"},';

$num++;

}

$data.=']';

}else{

$data.='"type":"view","url":"'.$vo['men1_url'].'"';

}

if($k==3){

$data.='}';

}else{

$data.='},';

}

$k++;

}

$data.=']}';

$url='https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$json->access_token;

// P($data);

//exit;

file_get_contents('https://api.weixin.qq.com/cgi-bin/menu/delete?access_token='.$json->access_token);

if($this->api_notice_increment($url,$data)==false){

$this->error('操作失败',U("System/defineMenu"));

}else{

$this->success('操作成功',U("System/defineMenu"));

}

exit;

}

//POST提交方法

function api_notice_increment($url, $data){

$ch = curl_init();

$header = "Accept-Charset: utf-8";

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

curl_setopt($curl, CURLOPT_HTTPHEADER, $header);

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);

// p($tmpInfo);

if (curl_errno($ch)) {

return false;

}else{

return true;

}

}

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值