微信公众平台接口开发初涉

1,试用测试账号开发http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login;
2,扫描登陆后,一些关键的信息appID,appsecret有用;
3,接口配置url和token,url里的php文件是用于处理用户对公众号的一些操作信息的收集和反馈,大部分的交互内容都是通过这个文件进行的

//define your token
define("TOKEN", "weixinceshi");
$wechatObj = new wechatCallbackapi();
if(0){        //我这里就不进行验证了
    $wechatObj->valid();    
}else{
    $wechatObj->responseMsg();//响应用户的操作
}

4,关于responseMsg都用户一些常见发送消息响应

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,对文本消息进行处理

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事件回应的

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,接口的参数里面必须的

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=全局返回码说明

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值