接入脚本interface.php实现代码

承接上文的WeChatCallBack

    在WeChatCallBack类的成员变量中定义了各种消息都会有的字段,这些字段在init函数中赋值。同时也把解析到的XML对象作为这个类的成员变量$_postObject并在init中赋值,目的是在实现具体公众账号的业务逻辑时,具体的各类消息的特殊字段可以通过它来获取。

    process函数时实现具体公众账号的业务逻辑时需要重载的函数,默认的实现是返回一个“未实现”的错误提示。

  本文的重点是接入脚本,接入脚本是项目根目录的interface.php,其代码清单如下文所示:

  <?php

  require_once dirname(__FILE__) . '/common/GlobalFunctions.php';

  function checkSingnature()

  {

    $signature = $_GET["signature"];

    $timestamp = $_GET["timestamp"];

    $nonce = $_GET["nonce"];

    $token = WEIXIN_TOKEN;

    $tmpArr = array($token, $timestamp, $nonce);

    sort($tmpArr);

    $tmpStr = implode( $tmpArr );

    $tmpStr = shal( $tmpStr );

    if( $tmpStr == $signature ) {

      return true;

    } else {

      return false;

    }

  }

  if(checkSignature()) {

    if($_GET["echostr"]) {

      echo $_GET["echostr"];

      exit(0);

    }

  } else {

    // 恶意请求: 获取来源IP, 并写入日志

    $ip = getIp();

    interface_log(ERROR, EC_OTHER, 'malicious: ' . $ip);

    exit(0);

  }

  function getWeChatObj($toUserName) {

    if($toUserName == USERNAME_FINDFACE) {

      require_once dirname(__FILE__) . '/class/

      WeChatCallBackMeiri10futu.php' ;

      return new WeChatCallBackMeir10futu();

    }

    if($toUserName == USERNAME_MR)  {

      require_once dirname(__FILE__) . '/class/

      WeChatCallBackMeir10futu.php' ;

      return new WeChatCallBackMeir10futu();

    }

    if($toUserName == USERNAME_ES) {

      require_once dirname(__FILE__) . '/class/

      WeChatCallBackEchoServer.php' ;

      return new WeChatCallBackEchoServer();

    }

    require_once dirname(__FILE__) . '/class/WeChatCallBcak.php' ;

    return new WeChatCallBack();

  }

  function exitErrorInput() {

    echo 'error input!' ;

    interface_log(INFO, EC_OK, "***** interface request end ******") ;

    interface_log(INFO, EC_OK, "********************** ******") ;

    interface_log(INFO, EC_OK, "") ;

    exit( 0 );

  }

  $postStr = file_get_contents ( "php://input" );

  

  interface_log(INFO, EC_OK, "");

  interface_log(INFO, EC_OK, "*****************************");

  interface_log(INFO, EC_OK, "***** interface request start *****");

  interface_log(INFO, EC_OK, 'request: '  .  var_export($_GET, ture)) ;

  if (empty ( $postStr )) {

    interface_log ( ERROR, EC_OK, "error input!" );

    exitErrorInput();

  }

  // 获取参数

  $postObj = simplexml_load_string  ( $postStr,  'SimpleXMLElement',

  LIBXML_NOCDATA ) ;

  $toUserName = ( string ) trim ( $postObj->ToUserName ) ;

  if (! $toUserName) {

    interface_log ( ERROR, EC_OK, "error input!" ) ;

    exitErrorInput() ;

  } else {

    $wechatObj = getWeChatObj ( $ toUserName ) ;

  }

  retStr = $ wechatObj->process ();

  interface_log(INFO, EC_OK, "***** interface request end *****") ;

  interface_log(INFO, EC_OK, "****************************") ;

  interface_log(INFO, EC_OK, "");

  ?>

  Interface.php逻辑描述:

  1.使用checkSignature函数验证请求是否合法,在不合法的情况下,记录恶意的来源IP。

  2.获取POST数据,解析XML数据,根据ToUserName来决定是发往哪一个公众账号的消息,然后加载对应的文件,获得对应的对象(getWeChatObj函数)。

  3.调用对象的init函数初始化对象。

  4.调用对象的process函数处理公众账号逻辑,得到返回消息字符串。

  5.返回消息。

  获取来源IP函数:

  function getIp () 

  {

    if (isset($_SERVER)) {

        if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {

            $realip = $_SERVER["HTTP_X_FORWARDED_FOR"] ;

        } else if (isser($_SERVER["HTTP_CLIENT_IP"]))  {

            $realip = $_SERVER["HTTP_CLIENT_IP"] ;

        } else {

            $realip = $SERVER["REMOTE_ADDR"];

        }

    } else {

        if (getenv(""HTTP_X_FORWARDED_FOR")) {

            $realip = getenv("HTTP_X_FORWARDED_FOR") ;

        } else if (getenv("HTTP_CLIENT_IP")) {

            $realip = getenv("HTTP_CLIENT_IP") ;

        } else {

            $realip = getenv("REMOVE_ADDR") ;

        }

    }

    return $realip;

  }

___________over____________

原文地址:https://www.cnblogs.com/iifranky7/p/3977127.html