码迷,mamicode.com
首页 > 微信 > 详细

微信自定义菜单

时间:2014-08-21 18:55:54      阅读:458      评论:0      收藏:0      [点我收藏+]

标签:blog   http   os   io   for   ar   div   cti   

<?php
/**
  * wechat php test
  */

//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();

$wechatObj->responseMsg();


$appid="XXXX";//填写appid
$secret="XXXXX";//填写secret

$wechatObj->create_menu($appid,$secret);//创建菜单


class wechatCallbackapiTest
{
	public function valid()
    {
        $echoStr = $_GET["echostr"];

        //valid signature , option
        if($this->checkSignature()){
        	echo $echoStr;
        	exit;
        }
    }
    public function responseMsg()
    {
		//get post data, May be due to the different environments
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

      	//extract post data
		if (!empty($postStr)){
                
              	$postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $keyword = trim($postObj->Content);
                $time = time();
                $textTpl = "<xml>
							<ToUserName><![CDATA[%s]]></ToUserName>
							<FromUserName><![CDATA[%s]]></FromUserName>
							<CreateTime>%s</CreateTime>
							<MsgType><![CDATA[%s]]></MsgType>
							<Content><![CDATA[%s]]></Content>
							<FuncFlag>0</FuncFlag>
							</xml>";             
				if(!empty( $keyword ))
                {
              		$msgType = "text";
                	$contentStr = "123Welcome to the Macau International Airport Micro message platform, Micro message is being tested, the related query to Micro message platform account wechat1@aims.com.mo associated query!~!~";
                	$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                	echo $resultStr;
                }else{
                	echo "Input something...";
                }

        }else {
        	echo "";
        	exit;
        }
    }
		
	private function checkSignature()
	{
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];	
        		
		$token = TOKEN;
		$tmpArr = array($token, $timestamp, $nonce);
		sort($tmpArr, SORT_STRING);
		$tmpStr = implode( $tmpArr );
		$tmpStr = sha1( $tmpStr );
		
		if( $tmpStr == $signature ){
			return true;
		}else{
			return false;
		}
	}
	
	
	public function get_access_token($appid,$secret) {
		//请求地址
		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
		$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);
		$a = curl_exec($ch);//获取地址
		curl_close($ch); //关闭
		$strjson=json_decode($a);//json解析
		$access_token = $strjson->access_token;//获取access_token
		
		return $access_token;
	}
	
	//创建菜单
	public  function   create_menu($appid,$secret){
	 
	 $access_token = $this->get_access_token($appid,$secret);
		
		//构建button
		$post= ‘{
        "button": [
        {
            "name": "航班信息", 
            "sub_button": [
                {
                    "type": "view", 
                    "name": "实事航班信息", 
                    "url": "http://www.macau-airport.com"
                }, 
                {
                    "type": "view", 
                    "name": "测试", 
                    "url": "http://www.macau-airport.com"
                },
                {
                    "type": "view", 
                    "name": "航班时刻表", 
                    "url": "http://www.macau-airport.com"
                }
            ]
        }, 
        {
            "name": "机场服务", 
            "sub_button": [
                {
                    "type": "view", 
                    "name": "旅行预订服务", 
                    "url": "http://www.macau-airport.com"
                }, 
                {
                    "type": "view", 
                    "name": "豪华专车服务", 
                    "url": "http://www.macau-airport.com"
                }, 
                {
                    "type": "view", 
                    "name": "直通快线服务", 
                    "url": "http://www.macau-airport.com"
                }
            ]
        }, 
        {
            "name": "机场设施", 
            "sub_button": [
                {
                    "type": "view", 
                    "name": "银行", 
                    "url": "http://www.macau-airport.com"
                }, 
                {
                    "type": "view", 
                    "name": "免税店", 
                    "url": "http://www.macau-airport.com"
                }, 
                {
                    "type": "view", 
                    "name": "百福小厨", 
                    "url": "http://www.macau-airport.com"
                }, 
                {
                    "type": "view", 
                    "name": "行李存放", 
                    "url": "http://www.macau-airport.com"
                }, 
                {
                    "type": "view", 
                    "name": "更多", 
                    "url": "http://www.macau-airport.com"
                }
            ]
        }
      ]}‘; //提交内容
		$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$access_token}"; //查询地址 
		
		
		$ch = curl_init();//新建curl
		curl_setopt($ch, CURLOPT_URL, $url);//url  
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($ch, CURLOPT_POST, 1);  //post
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post);//post内容  
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_exec($ch); //输出   
		curl_close($ch); //关闭
		
		
	}
}

?>

  

微信自定义菜单,布布扣,bubuko.com

微信自定义菜单

标签:blog   http   os   io   for   ar   div   cti   

原文地址:http://www.cnblogs.com/hgj123/p/3927609.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!