欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php版微信自定義回復(fù)功能示例

 更新時(shí)間:2016年12月05日 11:24:56   作者:牛逼的霍嘯林  
這篇文章主要介紹了php版微信自定義回復(fù)功能,結(jié)合完整實(shí)例形式分析了php版微信自定義回復(fù)功能的設(shè)置與代碼實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了php版微信自定義回復(fù)功能。分享給大家供大家參考,具體如下:

配置好服務(wù)器之后,就可以用php實(shí)現(xiàn)自動(dòng)回復(fù)了。

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); //獲取數(shù)據(jù)
      $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()); // 回復(fù)的內(nèi)容
        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
        echo $resultStr;
      }
    }else{
      echo "";
      exit;
    }
  }
}
?>

效果:

當(dāng)用戶輸入?或者?就會(huì)獲取當(dāng)前時(shí)間

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP微信開(kāi)發(fā)技巧匯總》、《PHP編碼與轉(zhuǎn)碼操作技巧匯總》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語(yǔ)法入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論