PHP头条
热点:

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


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


/**
  * wechat php test
  */

//define your token
define("TOKEN", "weixin");

$token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=***&secret=***";

$data = "{
     "button":[
     {
          "type":"click",
          "name":"今日歌曲",
          "key":"V1001_TODAY_MUSIC"
      },
      {
           "type":"click",
           "name":"歌手简介",
           "key":"V1001_TODAY_SINGER"
      },
      {
           "name":"菜单",
           "sub_button":[
            {
               "type":"click",
               "name":"hello word",
               "key":"V1001_HELLO_WORLD"
            },
            {
               "type":"click",
               "name":"赞一下我们",
               "key":"V1001_GOOD"
            }]
       }]
 }";

$wechatObj = new wechatCallbackapiTest();
$wechatObj->post($wechatObj->get_access_token(), $data);

class wechatCallbackapiTest
{
public function get_access_token(){  
        $json=http_request_json($token_url);
        $data=json_decode($json,true);  
        if($data['access_token']){  
            return $data['access_token'];  
        }else{  
            return "获取access_token错误";  
        }         
    }  

    public function http_request_json($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);  
        $result = curl_exec($ch);  
        curl_close($ch);  
        return $result;    
    }  

public function post($access_token, $jsonData){
$ch = curl_init("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token) ;
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,$jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

$result = curl_exec($ch) ;
curl_close($ch) ;
return $result;
}
}

?>
分享到: 更多

相关文章

相关频道:

PHP之友评论

今天推荐