Home  >  Article  >  php教程  >  php版微信公众平台账号自定义菜单类

php版微信公众平台账号自定义菜单类

PHP中文网
PHP中文网Original
2016-05-25 17:01:501378browse

wx_menu.php    

<?php
//define your token
define("TOKEN", "chenxiang");//改成自己的TOKEN
define(&#39;APP_ID&#39;, &#39;&#39;);//改成自己的APPID
define(&#39;APP_SECRET&#39;, &#39;&#39;);//改成自己的APPSECRET

$wechatObj = new wechatCallbackapiTest(APP_ID,APP_SECRET);
$wechatObj->Run();

class wechatCallbackapiTest
{
    private $fromUsername;
    private $toUsername;
    private $times;
    private $keyword;
    private $app_id;
    private $app_secret;


    public function __construct($appid,$appsecret)
    {
# code...
        $this->app_id = $appid;
        $this->app_secret = $appsecret;
    }
    public function valid()
    {
        $echoStr = $_GET["echostr"];
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }
    /**
     * 运行程序
     * @param string $value [description]
     */
    public function Run()
    {
        $this->responseMsg();
        $arr[]= "您好,这是自动回复,我现在不在,有事请留言,我会尽快回复你的^_^";
        echo $this->make_xml("text",$arr);
    }
    public function responseMsg()
    {   
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];//返回回复数据
        if (!empty($postStr)){
            $access_token = $this->get_access_token();//获取access_token
            $this->createmenu($access_token);//创建菜单
            //$this->delmenu($access_token);//删除菜单
            $postObj = simplexml_load_string($postStr, &#39;SimpleXMLElement&#39;, 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==&#39;event&#39;){
                $MsgEvent = $postObj->Event;//获取事件类型
                if ($MsgEvent==&#39;subscribe&#39;) {//订阅事件
                    $arr[] = "你好,我是xxx,现在我们是好友咯![愉快][玫瑰]";
                    echo $this->make_xml("text",$arr);
                    exit;
                }elseif ($MsgEvent==&#39;CLICK&#39;) {//点击事件
                    $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[&#39;access_token&#39;]){
            return $data[&#39;access_token&#39;];
        }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( 
                &#39;button&#39; =>array(
                    array(
                        &#39;name&#39;=>urlencode("生活查询"),
                        &#39;sub_button&#39;=>array(
                            array(
                                &#39;name&#39;=>urlencode("天气查询"),
                                &#39;type&#39;=>&#39;click&#39;,
                                &#39;key&#39;=>&#39;VCX_WEATHER&#39;
                                ),
                            array(
                                &#39;name&#39;=>urlencode("身份证查询"),
                                &#39;type&#39;=>&#39;click&#39;,
                                &#39;key&#39;=>&#39;VCX_IDENT&#39;
                                )
                            )
                        ),
                    array(
                        &#39;name&#39;=>urlencode("轻松娱乐"),
                        &#39;sub_button&#39;=>array(
                            array(
                                &#39;name&#39;=>urlencode("刮刮乐"),
                                &#39;type&#39;=>&#39;click&#39;,
                                &#39;key&#39;=>&#39;VCX_GUAHAPPY&#39;
                                ),
                            array(
                                &#39;name&#39;=>urlencode("幸运大转盘"),
                                &#39;type&#39;=>&#39;click&#39;,
                                &#39;key&#39;=>&#39;VCX_LUCKPAN&#39;
                                )
                            )
                        ),
                    array(
                            &#39;name&#39;=>urlencode("我的信息"),
                            &#39;sub_button&#39;=>array(
                                array(
                                    &#39;name&#39;=>urlencode("关于我"),
                                    &#39;type&#39;=>&#39;click&#39;,
                                    &#39;key&#39;=>&#39;VCX_ABOUTME&#39;
                                    ),
                                array(
                                    &#39;name&#39;=>urlencode("工作信息"),
                                    &#39;type&#39;=>&#39;click&#39;,
                                    &#39;key&#39;=>&#39;VCX_JOBINFORMATION&#39;
                                    )
                                )
                         )
                        )
                        );
        $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);
        curl_exec($ch);
        curl_close($ch);
    }
    /**
     * 查询菜单
     * @param $access_token 已获取的ACCESS_TOKEN
     */

    private function getmenu($access_token)
    {
# code...
        $url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".$access_token;
        $data = file_get_contents($url);
        return $data;
    }
    /**
     * 删除菜单
     * @param $access_token 已获取的ACCESS_TOKEN
     */

    private function delmenu($access_token)
    {
# code...
        $url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".$access_token;
        $data = json_decode(file_get_contents($url),true);
        if ($data[&#39;errcode&#39;]==0) {
# code...
            return true;
        }else{
            return false;
        }
    }

    /**
     *@param type: text 文本类型, news 图文类型
     *@param value_arr array(内容),array(ID)
     *@param o_arr array(array(标题,介绍,图片,超链接),...小于10条),array(条数,ID)
     */

    private function make_xml($type,$value_arr,$o_arr=array(0)){
        //=================xml header============
        $con="<xml>
            <ToUserName><![CDATA[{$this->fromUsername}]]></ToUserName>
            <FromUserName><![CDATA[{$this->toUsername}]]></FromUserName>
            <CreateTime>{$this->times}</CreateTime>
            <MsgType><![CDATA[{$type}]]></MsgType>";

        //=================type content============
        switch($type){

            case "text" : 
                $con.="<Content><![CDATA[{$value_arr[0]}]]></Content>
                    <FuncFlag>{$o_arr}</FuncFlag>";  
                    break;

            case "news" : 
                $con.="<ArticleCount>{$o_arr[0]}</ArticleCount>
                    <Articles>";
                foreach($value_arr as $id=>$v){
                    if($id>=$o_arr[0]) break; else null; //判断数组数不超过设置数
                    $con.="<item>
                        <Title><![CDATA[{$v[0]}]]></Title> 
                        <Description><![CDATA[{$v[1]}]]></Description>
                        <PicUrl><![CDATA[{$v[2]}]]></PicUrl>
                        <Url><![CDATA[{$v[3]}]]></Url>
                        </item>";
                }
                $con.="</Articles>
                    <FuncFlag>{$o_arr[1]}</FuncFlag>";  
                    break;

        } //end switch

        //=================end return============
        $con.="</xml>";

        return $con;
    }


    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];    

        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
}
?>

                   

                   

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn