• 技术文章 >后端开发 >php教程

    利用php开发微信之的关注事件

    不言不言2018-06-14 11:44:43原创865
    这篇文章主要为大家详细介绍了php微信开发之关注事件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    本文实例为大家分享了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;
        }
      }
    }
    
    ?>

    以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

    相关推荐:

    利用php嵌套数组拼接并解析json

    关于PHP strip_tags保留多个HTML标签

    以上就是利用php开发微信之的关注事件的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:php 微信 关注
    上一篇:关于thinkphp5.1 文件引入路径的问题 下一篇:关于thinkPHP框架动态配置的用法分析
    VIP课程(WEB全栈开发)

    相关文章推荐

    • 【腾讯云】年中优惠,「专享618元」优惠券!• PHP与MYSQL中UTF8编码的中文排序实例_PHP• php中__destruct与register_shutdown_function执行的先后顺序问题_PHP• PHP APC缓存配置、使用详解_PHP• win7下memCache的安装过程(具体操作步骤)_PHP• 用php实现选择排序的解决方法_PHP
    1/1

    PHP中文网