首页  >  文章  >  后端开发  >  php实现微信公众平台账号自定义菜单类_php技巧

php实现微信公众平台账号自定义菜单类_php技巧

WBOY
WBOY原创
2016-05-16 20:29:21886浏览

本文实例讲述了php实现微信公众平台账号自定义菜单类的方法。分享给大家供大家参考。具体分析如下:

微信公众平台服务号可申请自定义菜单了,其它的号暂时不支持自定义菜单了,这个不但可以使用api来操作,还可以直接在后台定义菜单与参数。

申请自定义菜单

服务号可以申请自定义菜单,使用QQ登录的公众号,可以升级为邮箱登录,使用邮箱登录的公众号,可以修改登录邮箱,群发消息可以同步到腾讯微博.

微信公众平台账号api程序

复制代码 代码如下:
//定义你的代币
Define("TOKEN", "chenshan");//改成自己的TOKEN
Define('APP_ID', '');//改成自己的APPID
Define('APP_SECRET', '');//改成自己的APPSECRET
 
$wechatObj = new wechatCallbackapiTest(APP_ID,APP_SECRET);
$wechatObj->Run();
 
类 wechatCallbackapiTest
{
    私有 $fromUsername;
    私有 $toUsername;
    私人$次;
    私有$关键字;
    私有$app_id;
    私人$app_secret;
    
    公共函数 __construct($appid,$appsecret)
    {
        # 代码...
        $this->app_id = $appid;
        $this->app_secret = $appsecret;
    }
    公共函数 valid()
    {
        $echoStr = $_GET["echostr"];
        if($this->checkSignature()){
            回声 $echoStr;
            出口;
        }
    }
    /**
     * 运行程序
     * @param string $value [description]
    */
    公共函数 Run()
    {
        $this->responseMsg();
        $arr[]= "您好,这是自动回复,我现在不在,有事请,留言我会加速回复你的^_^";
        echo $this->make_xml("text",$arr);
    }
    公共函数responseMsg()
    {   
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];//返回回复数据
        if (!emptyempty($postStr)){
                $access_token = $this->get_access_token();//获取access_token
                $this->createmenu($access_token);//创建菜单
                //$this->delmenu($access_token);//删除菜单
                $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $this->fromUsername = $postObj->FromUserName;//发送消息方ID
                $this->toUsername = $postObj->ToUserName;//接收消息方ID
                $this->keyword = trim($postObj->Content);//用户发送的消息
                $this->times = time();//发送时间
                $MsgType = $postObj->MsgType;//消息类型
                if($MsgType=='事件'){
                    $MsgEvent = $postObj->Event;//获取事件类型
                    if ($MsgEvent=='subscribe') {//订阅事件
                        $arr[] = "你好,我是xxx,现在我们是好友咯![愉快][玫瑰]";
                        echo $this->make_xml("text",$arr);
                        出口;
                    }elseif ($MsgEvent=='CLICK') {//点击事件
                        $EventKey = $postObj->EventKey;//菜单的自定义的key值,可以根据此值判断用户点击了什么内容,从而推送不同信息
                        $arr[] = $EventKey;
                        echo $this->make_xml("text",$arr);
                        exit;
                    }
                }
        }else {
            echo "this a file for weixin API!";
            exit;
        }
    }
    /**
     * 获取access_token
    */
    private function get_access_token()
    {
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->app_id."&secret=".$this->app_secret;
        $data = json_decode(file_get_contents($url),true);
        if($data['access_token']){
            return $data['access_token'];
        }else{
            return "获取access_token错误";
        }
    }
    /**
     * 创建菜单
     * @param $access_token 已获取的ACCESS_TOKEN
    */
    public function createmenu($access_token)
    {
        $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;
        $arr = array( 
            'button' =>array(
                array(
                    'name'=>urlencode("生活查询"),
                    'sub_button'=>array(
                        array(
                            'name'=>urlencode("天气查询"),
                            'type'=>'click',
                            'key'=>'VCX_WEATHER'
                        ),
                        array(
                            'name'=>urlencode("身份证查询"),
                            'type'=>'click',
                            'key'=>'VCX_IDENT'
                        )
                    )
                ),
                array(
                    'name'=>urlencode("轻松娱乐"),
                    'sub_button'=>array(
                        array(
                            'name'=>urlencode("刮刮乐"),
                            'type'=>'click',
                            'key'=>'VCX_GUAHAPPY'
                        ),
                        array(
                            'name'=>urlencode("幸运大转盘"),
                            'type'=>'click',
                            'key'=>'VCX_LUCKPAN'
                        )
                    )
                ),
                array(
                    'name'=>urlencode("我的信息"),
                    'sub_button'=>array(
                        array(
                            'name'=>urlencode("关于我"),
                            'type'=>'click',
                            'key'=>'VCX_ABOUTME'
                        ),
                        array(
                            'name'=>urlencode("工作信息"),
                            'type'=>'click',
                            'key'=>'VCX_JOBINFORMATION'
                        )
                    )
                )
            )
        );
        $jsondata = urldecode(json_encode($arr));
        $ch=curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_POST,1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$jsondata);
        卷曲执行($ch);
        卷曲_关闭($ ch);
    }
    /**
     * 查询菜单
     * @param $access_token 已获取的ACCESS_TOKEN
    */
    
    私有函数 getmenu($access_token)
    {
        # 代码...
        $url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".$access_token;
        $data = file_get_contents($url);
        返回$数据;
    }
    /**
     * 删除菜单
     * @param $access_token 已获取的ACCESS_TOKEN
    */
    
    私有函数 delmenu($access_token)
    {
        # 代码...
        $url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".$access_token;
        $data = json_decode(file_get_contents($url),true);
        if ($data['errcode']==0) {
            # 代码...
            返回真;
        }其他{
            返回假;
        }
    }
        
    /**
     *@param type: text 文本类型, news 图文类型
     *@param value_arr array(内容),array(ID)
     *@param o_arr array(array(标题,介绍,图片,超链接),...小于10条),array(条数,ID)
    */
    
    私有函数 make_xml($type,$value_arr,$o_arr=array(0)){
        //==================xml header============
        $con="
                    fromUsername}]]>
                    toUsername}]]>
                    {$this->times}
                    ";
                    
          //==================输入内容============
        开关($类型){
          
            案例“文本”:
                $con.="<内容>
                    {$o_arr}";  
            休息;
            
            案例“新闻”:
                $con.="{$o_arr[0]}
                     ";
                foreach($value_arr as $id=>$v){
                    if($id>=$o_arr[0]) 中断;否则为空; //判断批量数不超过设置数
                    $con.="
                         标题> 
                         <描述>
                        
                        
                        
";
                }
                $con.="
                     {$o_arr[1]}";  
            休息;
        } //结束开关
         //==================结束返回============
        $con.="
";
        返回$con;
    }
 
    私有函数 checkSignature()
    {
        $signature = $_GET["签名"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];    
                
        $令牌=令牌;
        $tmpArr = 数组($token, $timestamp, $nonce);
        排序($tmpArr);
        $tmpStr = 内爆( $tmpArr );
        $tmpStr = sha1( $tmpStr );
        
        if( $tmpStr == $signature ){
            返回真;
        }其他{
            返回假;
        }
    }
}
?>

希望本文所述对大家基于PHP的微信开发有所帮助。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn