源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

php版微信自定义回复功能示例

  • 时间:2020-10-16 12:18 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:php版微信自定义回复功能示例
本文实例讲述了php版微信自定义回复功能。分享给大家供大家参考,具体如下: [img]http://files.jb51.net/file_images/article/201612/2016125113848833.png?2016115113916[/img] 配置好服务器之后,就可以用php实现自动回复了。 index.php中的代码
<?php
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
if (isset($_GET['echostr'])) {
  $wechatObj->valid();
}else{
  $wechatObj->responseMsg();
}
class wechatCallbackapiTest
{
  public function valid()
  {
    $echoStr = $_GET["echostr"];
    if($this->checkSignature()){
      header('content-type:text');
      echo $echoStr;
      exit;
    }
  }
  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;
    }
  }
  public function responseMsg()
  {
    $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
    if (!empty($postStr)){
      $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); //获取数据
      $fromUsername = $postObj->FromUserName;
      $toUsername = $postObj->ToUserName;
      $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($keyword == "?" || $keyword == "?") //获取用户信息
      {
        $msgType = "text";
        $contentStr = date("Y-m-d H:i:s",time()); // 回复的内容
        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
        echo $resultStr;
      }
    }else{
      echo "";
      exit;
    }
  }
}
?>

效果: 当用户输入?或者?就会获取当前时间 [img]http://files.jb51.net/file_images/article/201612/2016125113919645.png?2016115113940[/img] 更多关于PHP相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/556.htm]PHP微信开发技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/459.htm]PHP编码与转码操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/495.htm]PHP网络编程技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/348.htm]PHP基本语法入门教程[/url]》、《[url=http://www.1sucai.cn/Special/47.htm]php字符串(string)用法总结[/url]》、《[url=http://www.1sucai.cn/Special/84.htm]php+mysql数据库操作入门教程[/url]》及《[url=http://www.1sucai.cn/Special/231.htm]php常见数据库操作技巧汇总[/url]》 希望本文所述对大家PHP程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部