微信公衆平臺接口開發初涉

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

相關文章
相關標籤/搜索