微信入门教学之自动回复和自定义菜单

1.首先注册一个微信公众号,然后登陆  https://mp.weixin.qq.com

2.登陆成功以后有订阅号、测试号等,订阅号很多功能没有权限,这里我们选择测试号

  “开发者工具------>公众平台测试账号”,点击进入,登陆到测试账号

3.对于刚接触微信开发,我们打开微信开发手册,先看接入指南,下载demo

4.demo放到我们的服务器上直接运行,这里先跳过服务器地址的验证,直接return true;

5.现在我们开始对接,微信公众平台的“测试账号管理----->接口配置信息修改”,我们点击 修改,要确保token demo 里常量token的值保持一致,点击配置,然后配置成功。

6.这一步我们开始学习微信自动回复。

public function valid($arr)
    {
        //var_dump($arr);die;
        $datas=$arr;
        $echoStr = $_GET["echostr"];
        //valid signature , option
        if($this->checkSignature()){
        	echo $echoStr;
             $this->responseMsg($datas);    //调用消息回复的方法
        	exit;
        }
    }

    public function responseMsg($datas)
    {
        //var_dump($datas);die;
		//get post data, May be due to the different environments
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];  //判断微信是否接收到请求

      	//extract post data
		if (!empty($postStr)){
                /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
                   the best way is to check the validity of xml by yourself */
                libxml_disable_entity_loader(true);
              	$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $keyword = trim($postObj->Content);
                $time = time();
                $textTpl = "<xml>                     //将要回复的类型写出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 ))
                {
                    foreach($datas as $k=>$v){
                        if($keyword==$v['keywords']){
                    $msgType = "text";            //定义要回复的类型,这里是文本
                	$contentStr = $v['backwords']; //定义回复的内容,这里从数据库取值                      
                	$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                	echo $resultStr;
                    }
                    }
              		
                }else{
                	echo "Input something...";
                }

        }else {
        	echo "";
        	exit;
        }
}

到这里消息自动回复就完成了,大家可以登录微信关注测试号验证

 

 

1.我们接下来学习的是微信自定义菜单,这里我们细分为如下几步

      1)首先获取access_token

            根据AppIdAppSecret调用接口(get请求)

            

$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".appID."&secret=".appsecret;
        $json=file_get_contents($url);
        $arr=json_decode($json,true);
        $Accesstoken=$arr['access_token'];   //获得access_token(一天有2000次,我们可    以做个缓存)

     (2)根据access_token 调用接口(post请求,我们用curl模拟post请求)
        $url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$accesstoken;
         $data='{
     "button":[
     {	
		 	"type":"view",
            "name":"面试中心",
            "url":"http://www.mbaodian.com"
      },
      {
           "name":"操作大全",
           "sub_button":[
           {	
                    "type": "pic_photo_or_album", 
                    "name": "拍照或者相册发图", 
                    "key": "rselfmenu_1_1", 
                    "sub_button": [ ]
            },
            {
		    		"type":"view",
                    "name":"微信开发",
                    "url":"http://www.haoyunyun.cn/baodiankaifa/sample.php"
            },
            {
				"name": "发送位置", 
            	"type": "location_select", 
            	"key": "rselfmenu_2_0"               
            }]
       }]
 }';
 $file = $this->curlPost($url,$data,'POST');
 Echo $file;           //如果输出{“errcode”:0,”errmsg”:”ok”}表示成功
    }
    public function curlPost($url,$data,$method){
        $ch = curl_init();   //1.初始化
        curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式
        //4.参数如下
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');//模拟浏览器
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));//gzip解压内容
        curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
        if($method=="POST"){//5.post方式的时候添加数据
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);//6.执行
        if (curl_errno($ch)) {//7.如果出错
            return curl_error($ch);
        }
        curl_close($ch);//8.关闭
        return $tmpInfo;
}

自定义菜单就完成了


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值