search
Homephp教程php手册微信公众平台开发关注及取消关注事件的方法,公众平台

微信公众平台开发关注及取消关注事件的方法,公众平台

本文实例讲述了微信公众平台开发关注及取消关注事件的方法。分享给大家供大家参考。具体分析如下:

用户在关注与取消关注公众号时,微信会把这个事件推送到开发者填写的URL,方便开发者给用户下发欢迎消息或者做帐号的解绑.

下面是一个微信公众平台关注和取消关注的实例,代码如下:

复制代码 代码如下:

define("TOKEN", "w3note");//定义识别码
$wechatObj = new wechatCallbackapiTest();//实例化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()//执行接收器方法
    {
    $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 = "欢迎关注网志博客";//这里是向关注者发送的提示信息
    break;
    case "unsubscribe":
    $content = "";
    break;
   }
   $result = $this->transmitText($object,$content);
   return $result;
    }
 private function transmitText($object,$content){
   $textTpl = "
      
      
       %s
      
      
       0
      
";
    $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;
  }
 }
}


代码相关参数说明:
参数 描述
ToUserName 开发者微信号
FromUserName 发送方帐号(一个OpenID)
CreateTime 消息创建时间 (整型)
MsgType  消息类型,event
Event 事件类型,subscribe(订阅)、unsubscribe(取消订阅)

希望本文所述对大家的php程序设计有所帮助。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!