微信 php 自定义菜单,该如何解决

WBOY
发布: 2016-06-13 12:30:51
原创
901 人浏览过

微信 php 自定义菜单
我申请了一个微信公众平台的测试账号,之前已经通过验证,关注后用我的微信号向测试账号发消息可以得到测试账号的正确响应,我现在想实现微信的自定义菜单,下面用***代替了我测试账号的appid和secret,我进测试账号没看到自定义菜单,请问问题出在哪?

<br />
<?php<br />
/**<br />
  * wechat php test<br />
  */<br />
<br />
//define your token<br />
define("TOKEN", "weixin");<br />
<br />
$token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=***&secret=***";<br />
<br />
$data = "{<br />
     "button":[<br />
     {	<br />
          "type":"click",<br />
          "name":"今日歌曲",<br />
          "key":"V1001_TODAY_MUSIC"<br />
      },<br />
      {<br />
           "type":"click",<br />
           "name":"歌手简介",<br />
           "key":"V1001_TODAY_SINGER"<br />
      },<br />
      {<br />
           "name":"菜单",<br />
           "sub_button":[<br />
            {<br />
               "type":"click",<br />
               "name":"hello word",<br />
               "key":"V1001_HELLO_WORLD"<br />
            },<br />
            {<br />
               "type":"click",<br />
               "name":"赞一下我们",<br />
               "key":"V1001_GOOD"<br />
            }]<br />
       }]<br />
 }";<br />
<br />
$wechatObj = new wechatCallbackapiTest();<br />
$wechatObj->post($wechatObj->get_access_token(), $data);<br />
<br />
class wechatCallbackapiTest<br />
{	<br />
	public function get_access_token(){  <br />
        $json=http_request_json($token_url);<br />
        $data=json_decode($json,true);  <br />
        if($data['access_token']){  <br />
            return $data['access_token'];  <br />
        }else{  <br />
            return "获取access_token错误";  <br />
        }         <br />
    }  <br />
	<br />
    public function http_request_json($url){    <br />
        $ch = curl_init();  <br />
        curl_setopt($ch, CURLOPT_URL,$url);  <br />
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  <br />
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  <br />
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  <br />
        $result = curl_exec($ch);  <br />
        curl_close($ch);  <br />
        return $result;    <br />
    }  <br />
	<br />
	public function post($access_token, $jsonData){<br />
		$ch = curl_init("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token) ;<br />
		curl_setopt($ch, CURLOPT_POST, 1);<br />
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");<br />
		curl_setopt($ch, CURLOPT_POSTFIELDS,$jsonData);<br />
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);<br />
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));<br />
<br />
		$result = curl_exec($ch) ;<br />
		curl_close($ch) ;<br />
		return $result;<br />
	}<br />
}<br />
<br />
?><br />
登录后复制

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板