欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信公眾平臺開發(fā)關(guān)注及取消關(guān)注事件的方法

 更新時間:2014年12月23日 11:58:08   投稿:shichen2014  
這篇文章主要介紹了微信公眾平臺開發(fā)關(guān)注及取消關(guān)注事件的方法,較為詳細(xì)的分析了微信公眾平臺設(shè)置關(guān)注的技巧,并附帶了相關(guān)參數(shù)的說明,具有一定的參考借鑒價值,需要的朋友可以參考下

本文實(shí)例講述了微信公眾平臺開發(fā)關(guān)注及取消關(guān)注事件的方法。分享給大家供大家參考。具體分析如下:

用戶在關(guān)注與取消關(guān)注公眾號時,微信會把這個事件推送到開發(fā)者填寫的URL,方便開發(fā)者給用戶下發(fā)歡迎消息或者做帳號的解綁.

下面是一個微信公眾平臺關(guān)注和取消關(guān)注的實(shí)例,代碼如下:

復(fù)制代碼 代碼如下:
define("TOKEN", "w3note");//定義識別碼
$wechatObj = new wechatCallbackapiTest();//實(shí)例化wechatCallbackapiTest類
 
if(!isset($_GET["echostr"])){
     $wechatObj->responseMsg();
}else{
 $wechatObj->valid();
}
class wechatCallbackapiTest
{
 public function valid()
    {
        $echoStr = $_GET["echostr"];
        if($this->checkSignature()){
         echo $echoStr;
         exit;
        }
    }
    public function responseMsg()//執(zhí)行接收器方法
    {
    $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
    if (!emptyempty($postStr)){
      $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
      $RX_TYPE = trim($postObj->MsgType);
      switch($RX_TYPE){
       case "event":
       $result = $this->receiveEvent($postObj);
       breadk;
      }
      echo $result;
  }else{
   echo "";
   exit;
  }
 }
  private function receiveEvent($object){
   $content = "";
   switch ($postObj->Event){
    case "subscribe":
    $content = "歡迎關(guān)注網(wǎng)志博客";//這里是向關(guān)注者發(fā)送的提示信息
    break;
    case "unsubscribe":
    $content = "";
    break;
   }
   $result = $this->transmitText($object,$content);
   return $result;
    }
 private function transmitText($object,$content){
   $textTpl = "<xml>
       <ToUserName><![CDATA[%s]]></ToUserName>
       <FromUserName><![CDATA[%s]]></FromUserName>
       <CreateTime>%s</CreateTime>
       <MsgType><![CDATA[text]]></MsgType>
       <Content><![CDATA[%s]]></Content>
       <FuncFlag>0</FuncFlag>
       </xml>";
    $result = sprintf($textTpl, $object->FromUserName, $object->$ToUserName, time(), $content);
    return $result;
  }
 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;
  }
 }
}

代碼相關(guān)參數(shù)說明:

參數(shù) 描述
ToUserName 開發(fā)者微信號
FromUserName 發(fā)送方帳號(一個OpenID)
CreateTime 消息創(chuàng)建時間 (整型)
MsgType  消息類型,event
Event 事件類型,subscribe(訂閱)、unsubscribe(取消訂閱)

希望本文所述對大家的php程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論