当前位置: 移动技术网 > IT编程>开发语言>PHP > php微信开发之关注事件

php微信开发之关注事件

2018年08月10日  | 移动技术网IT编程  | 我要评论

桃子影视,阳江网络问政平台,acquire的用法

本文实例为大家分享了php微信关注事件的具体代码,供大家参考,具体内容如下

<?php
/**
 * wechat php test
 */

//define your token
define("token", "weixin");
$wechatobj = new wechatcallbackapitest();
//$wechatobj->valid();
$wechatobj->responsemsg();
class wechatcallbackapitest
{
  public function valid()
  {
    $echostr = $_get["echostr"];

    //valid signature , option
    if($this->checksignature()){
      echo $echostr;
      exit;
    }
  }

  public function responsemsg()
  {
    //get post data, may be due to the different environments
    $poststr = $globals["http_raw_post_data"];

    //extract post data
    if (!empty($poststr)){

        $postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata);
        $fromusername = $postobj->fromusername;
        $tousername = $postobj->tousername;
        $type = $postobj->msgtype;
        $customevent = $postobj->event;
        $keyword = trim($postobj->content);
        $time = time();
        $texttpl = "<xml>
              <tousername><![cdata[%s]]></tousername>
              <fromusername><![cdata[%s]]></fromusername>
              <createtime>%s</createtime>
              <msgtype><![cdata[%s]]></msgtype>
              <content><![cdata[%s]]></content>
              <funcflag>0</funcflag>
              </xml>";       
        if($type=="event" and $customrevent=="subscribe"){
          $contentstr = "感谢你的关注\n回复1查看联系方式\n回复2查看最新资讯\n回复3查看法律文书";
          $msgtype = "text";
          $resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
          echo $resultstr;
          }  
        if(!empty( $keyword ))
        {                
          $msgtype = "text";
          if($keyword=="1"){
          $contentstr = "qiphon";}
          if($keyword=="2"){
          $contentstr = "test 。";}
          if($keyword=="3"){
          $contentstr = "test333";}         
          $resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
          echo $resultstr;
        }else{
          echo "input something...";
        }

    }else {
      echo "";
      exit;
    }
  }

  private function checksignature()
  {
    $signature = $_get["signature"];
    $timestamp = $_get["timestamp"];
    $nonce = $_get["nonce"];  

    $token = token;
    $tmparr = array($token, $timestamp, $nonce);
    sort($tmparr);
    $tmpstr = implode( $tmparr );
    $tmpstr = sha1( $tmpstr );

    if( $tmpstr == $signature ){
      return true;
    }else{
      return false;
    }
  }
}

?>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网