PHP头条
热点:

接入脚本interface.php实现代码,脚本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____________

 


什是SP接入代码

是第二类增值电信业务中的移动网信息服务业务的短消息接入代码,分为全网和地网两类,其中全网sp接入代码为1066开头的全网经营性sp接入代码,和1069开头的全网非经营性sp接入代码;地网sp接入代码分为1062开头经营性和1063开头非经营性。全网sp接入代码的申请机构是工业和信息化部,地网接入代码的申请机构是地方的通信管理局,那么申请sp接入代码需要提交哪些资料呢? sp接入代码申请材料1、短消息类服务接入代码申请报告(号码位长、适用范围、联系人联系电话);2、短消息类服务接入代码(省内使用)申请表;3、短消息类服务接入代码使用承诺书;4、增值电信业务经营许可证(业务类别:短信息服务)原件及复印件;5、营业执照副本原件及复印件;6、技术方案、业务规划等。
 

接入代码是什

每个地区的接入代码是不同的
你只能拨打10086转人工服务了
或者你告诉我电话号码归属地才能帮你查询!
 

www.phpzy.comtrue/php/15067.htmlTechArticle接入脚本interface.php实现代码,脚本interface.php 承接上文的WeChatCallBack 在WeChatCallBack类的成员变量中定义了各种消息都会有的字段,这些字段在init函数中赋值。同时也把解析到的XML对象作...

相关文章

    暂无相关文章
相关频道:

PHP之友评论

今天推荐