1,试用测试帐号开发http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login;
2,扫描登录后,一些关键的信息appID,appsecret有用;
3,接口配置url和token,url里的php文件是用于处理用户对公众号的一些操做信息的收集和反馈,大部分的交互内容都是经过这个文件进行的php
//define your token define("TOKEN", "weixinceshi"); $wechatObj = new wechatCallbackapi(); if(0){ //我这里就不进行验证了 $wechatObj->valid(); }else{ $wechatObj->responseMsg();//响应用户的操做 }
4,关于responseMsg都用户一些常见发送消息响应json
public function responseMsg() { $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!empty($postStr)){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $RX_TYPE = trim($postObj->MsgType); switch ($RX_TYPE) { case 'text': $this->handleText($postObj); break; case 'image': $this->handleImage($postObj); break; case 'event': $this->handleEvent($postObj); break; default: $this->handleError($postObj); break; } }else { echo ""; exit; } }
5,对文本消息进行处理api
public function handleText($postObj) { $fromUsername = $postObj->FromUserName;//用户的openId $toUsername = $postObj->ToUserName; //服务号的 $time = time(); //消息发送时间 $contentStr = "hello world"; //内容 $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>".$time."</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content>".$contentStr."</Content> </xml>"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr;//return 也能够 }
6,对事件的处理,和文本不一样,这里面的命令(或者key)是经过CLICK事件回应的app
public function handleEvent($postObj) { $contentStr = ""; $ToUserName = $postObj->ToUserName; $FromUserName = $postObj->FromUserName; $time = time(); switch ($postObj->Event) { case "subscribe"://点击关注(订阅)激活的事件 $MsgType="news"; $tpl="<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>".$time."</CreateTime> <MsgType><![CDATA[news]]></MsgType> <ArticleCount>2</ArticleCount> <Articles> <item> <Title>感谢你关注xxx000</Title> <Description>welcome to xxx000!</Description> <PicUrl></PicUrl> <Url></Url> </item> </Articles> </xml> "; $resultStr = sprintf($tpl,$FromUserName,$ToUserName,$time,$MsgType); echo $resultStr; break; case "CLICK"://CLICK事件 $key=$postObj->EventKey;//获取菜单里面定义的key if($key=="你好"){ $contentStr = "你点的是click事件,内容是你好"; } $MsgType="text"; $tpl="<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>".$time."</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content>".$contentStr."</Content> </xml>"; $resultStr = sprintf($tpl,$FromUserName,$ToUserName,$time,$MsgType,$contentStr); echo $resultStr; break; default : $contentStr = "Unknow Event: ".$postObj->Event; break; } }
7,获取access_token,接口的参数里面必须的post
function get_access_token(){ $appid=""; //上面第2条有说明 $secret=""; $TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret; $result = json_decode(file_get_contents($TOKEN_URL),true); $token=$result['access_token']; //printf($access_token); return $token; }
8,建立,查询,修改自定义菜单测试
class Menu{ public function creat_Menu(){ //建立菜单 $access_token=get_access_token(); $creat_url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token; $data='{ "button":[ { "type":"click", "name":"今日推荐", "key":"你好" }, { "name":"商品搜索", "sub_button":[ { "type":"view", "name":"官方主页", "url":"http://www.baidu.com/" }, { "type":"view", "name":"top10推荐", "url":"wwww.qq.com/" }, { "type":"click", "name":"赞一个", "key":"V1001_GOOD" }] }] }'; $result=do_post($creat_url,$data); printf($result); echo '建立菜单'; } public function get_Menu(){ //获取菜单 $access_token=get_access_token(); $url1='https://api.weixin.qq.com/cgi-bin/menu/get?access_token='.$access_token; $result = file_get_contents($url1); printf($result); echo '获取菜单'; } public function delete_Menu(){ //删除菜单 $access_token=get_access_token(); $url1='https://api.weixin.qq.com/cgi-bin/menu/delete?access_token='.$access_token; $result = file_get_contents($url1); printf($result); echo '删除菜单'; } } ?>
9,最重要的,就是返回的状态码,由于调试有点费劲
http://mp.weixin.qq.com/wiki/index.php?title=全局返回码说明this