php微信公众号开发之简答题

本文实例为大家分享了php微信公众号开发简答题的具体代码,供大家参考,具体内容如下

简答题

  • array("第5页 第1个字" => "王","第8页 第1个字" => "李","第30页 第1个字" => "周",)
  • array_rand($array,1)
  • 数据库建立二级菜单

php微信公众号开发之简答题_第1张图片

php微信公众号开发之简答题_第2张图片

核心代码如下:

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;
        $latitude = $postObj->Location_X;
        $longitude = $postObj->Location_Y;
        $keyword = trim($postObj->Content);
        $time = time();
        $textTpl = "
              
              
              %s
              
              %s
              0
              "; 
        include("coon.php");      

        if($keyword=="8")
        {
          $array=array("第5页第1个字"=>"王","第8页第1个字"=>"李","第30页第1个字"=>"周");
          $a=array_rand($array,1);
          $b=$array[$a];
          $contentStr=$a."是什么?";
          $sql="INSERT INTO `menu2` (`id` ,`user` ,`sec`,`answer`)VALUES (NULL , '{$fromUsername}', '8','{$b}')";
          mysql_query($sql);
        }  
        else
        {
          $sql="SELECT * FROM `menu2` where `user`= '{$fromUsername}'";
          $query=mysql_query($sql);
          $rm=mysql_fetch_array($query);
          $sec=$rm['sec'];
          $answer=$rm['answer'];   
          if($sec=="8")
          {
            if($keyword==$answer)
            {
              $contentStr="你的答案是正确的,视频代码下载地址为....";
            }
            else
            {
              $contentStr="你的答案不正确,请购买《微信公众平台搭建与开发揭秘》";
            }
          }
          else 
          {
            $contentStr="请先输入8";
          }
        }
        $msgType="text";
        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
        echo $resultStr;
    }
  }

coon.php:连接数据库代码如下:

 
 

index.php整体代码如下:

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;
        $latitude = $postObj->Location_X;
        $longitude = $postObj->Location_Y;
        $keyword = trim($postObj->Content);
        $time = time();
        $textTpl = "
              
              
              %s
              
              %s
              0
              "; 
        include("coon.php");      

        if($keyword=="8")
        {
          $array=array("第5页第1个字"=>"王","第8页第1个字"=>"李","第30页第1个字"=>"周");
          $a=array_rand($array,1);
          $b=$array[$a];
          $contentStr=$a."是什么?";
          $sql="INSERT INTO `menu2` (`id` ,`user` ,`sec`,`answer`)VALUES (NULL , '{$fromUsername}', '8','{$b}')";
          mysql_query($sql);
        }  
        else
        {
          $sql="SELECT * FROM `menu2` where `user`= '{$fromUsername}'";
          $query=mysql_query($sql);
          $rm=mysql_fetch_array($query);
          $sec=$rm['sec'];
          $answer=$rm['answer'];   
          if($sec=="8")
          {
            if($keyword==$answer)
            {
              $contentStr="你的答案是正确的,视频代码下载地址为....";
            }
            else
            {
              $contentStr="你的答案不正确,请购买《微信公众平台搭建与开发揭秘》";
            }
          }
          else 
          {
            $contentStr="请先输入8";
          }
        }
        $msgType="text";
        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
        echo $resultStr;
    }
  }

  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微信公众号开发之简答题)