自定义菜单(IMOOC高级篇)

来源:互联网 发布:从零开始学淘宝美工 编辑:程序博客网 时间:2024/06/11 19:07

创建菜单栏:

   public  function defineItem(){        header('content-type:text/html;charset=utf-8');        $access_token= getWxAccessToken();        $url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;        /*$postArr= array(            'botton'=>array(                array(                    'name'=>'caidanyi',                    'type'=>'click',                    'key'=>'item1',                    ),                array(                    'name'=>'caidan2',                    'sub_button'=>array(                         array(                            'name'=>'song',                            'type'=>'click',                            'key'=>'songs',                            ),                         array(                            'name'=>'movie',                            'type'=>'view',                            'url'=>'http://www.qq.com',                            ),                    ),                    ),               array(                      'name'=>'caidansan',                      'type'=>'view',                      'url'=>'http://www.baidu.com',                    )               ),              );*/$postArr=array(            'button'=>array(                array(                    'name'=>urlencode('a1'),                    'type'=>'click',                    'key'=>'item1',                ),                array(                    'name'=>urlencode('a2'),                    'sub_button'=>array(                        array(                            'name'=>urlencode('歌曲'),                            'type'=>'click',                            'key'=>'songs'                        ),//第一个二级菜单                        array(                            'name'=>urlencode('电影'),                            'type'=>'view',                            'url'=>'http://www.baidu.com'                        ),//第二个二级菜单                    )                ),                array(                    'name'=>urlencode('a3'),                    'type'=>'view',                    'url'=>'http://www.qq.com',                ),//第三个一级菜单        ));        $postJson=json_encode($postArr);        var_dump("$postJson");        $res=$this->curl_post($url,$postJson);        var_dump($res);    }      public function curl_post($url,$data=''){    $curl = curl_init(); // 启动一个CURL会话          curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址                      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查          curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在          curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器          curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转          curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer          curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求          curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包          curl_setopt($curl, CURLOPT_COOKIEFILE, $GLOBALS['cookie_file']); // 读取上面所储存的Cookie信息          curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环          curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容          curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回          $tmpInfo = curl_exec($curl); // 执行操作          if (curl_errno($curl)) {             echo 'Errno'.curl_error($curl);          }          curl_close($curl); // 关键CURL会话          return $tmpInfo; // 返回数据    }

根据按钮的按键回复相应的文本

 public function responseMsg(){        $postArr = $GLOBALS['HTTP_RAW_POST_DATA'];        $postObj = simplexml_load_string( $postArr );        $toUser   = $postObj->FromUserName;                $fromUser = $postObj->ToUserName;                $time     = time();                $msgType  =  'text';                 $content='null';                $template = "<xml>                            <ToUserName><![CDATA[%s]]></ToUserName>                            <FromUserName><![CDATA[%s]]></FromUserName>                            <CreateTime>%s</CreateTime>                            <MsgType><![CDATA[%s]]></MsgType>                            <Content><![CDATA[%s]]></Content>                            </xml>";        if( strtolower($postObj->Event == 'CLICK') ){                 if( strtolower($postObj->EventKey == 'item1') ){                    $content='zheshi item1';            }             if( strtolower($postObj->EventKey == 'songs') ){                    $content='zheshi item2';            }    }    if( strtolower($postObj->Event == 'VIEW') ){        $content="link=".$postObj->EventKey;                  }                  echo sprintf($template, $toUser, $fromUser, $time, $msgType, $content);}

1注意返回的时间参数(EVENT的判断)是CLICK 不是click 因为一直用小写所以无法回复
2echo sprinf一次以后不会再执行后面的语句
3如果有跳转的菜单栏不会文本推送

0 0
原创粉丝点击