主頁 > 知識庫 > php微信公眾號開發(fā)之答題連闖三關(guān)

php微信公眾號開發(fā)之答題連闖三關(guān)

熱門標(biāo)簽:400 電話 辦理 蘇州外呼系統(tǒng)有效果嗎 海南外呼系統(tǒng)方案 地圖標(biāo)注怎么做商戶驗(yàn)證 亳州企業(yè)外呼系統(tǒng) 山東電銷卡外呼系統(tǒng)原理是什么 智能電銷語音機(jī)器人資訊 兼職做地圖標(biāo)注好賺錢嗎 打開百度地圖標(biāo)注

本文實(shí)例為大家分享了php微信公眾號開發(fā)之答題闖關(guān)的具體代碼,供大家參考,具體內(nèi)容如下

連闖三關(guān)

  • implode( "," ,array) 數(shù)組變字符串 數(shù)組無法保存到數(shù)據(jù)庫中去
  • explode(",",str) 字符串變數(shù)組
  • array_rand($arr,n ) 隨機(jī)取出數(shù)組中 n 個(gè)單項(xiàng)

核心代碼如下:

 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 = "xml>
        ToUserName>![CDATA[%s]]>/ToUserName>
        FromUserName>![CDATA[%s]]>/FromUserName>
        CreateTime>%s/CreateTime>
        MsgType>![CDATA[%s]]>/MsgType>
        Content>%s/Content>
        FuncFlag>0/FuncFlag>
       /xml>"; 
    include("coon.php");   
    $array=array("河北"=>"石家莊","山西"=>"太原","廣東"=>"廣州","黑龍江"=>"哈爾濱","廣西"=>"南寧","浙江"=>"杭州","江蘇"=>"南京","山東"=>"濟(jì)南","河南"=>"鄭州");
    if($keyword=="8")
    {
     $a=array_rand($array,3);
     $contentStr="第一題".$a[0]."省會(huì)是哪里?";
     $a=implode(",",$a); 
     $sql="SELECT * FROM `menu2` where `user`= '{$fromUsername}'";
     $query=mysql_query($sql);
     $rm=mysql_fetch_array($query); 
     $user=$rm['user'];
     if (empty($user))
     {
      $sql="INSERT INTO `menu2` (`id` ,`user` ,`sec`,`answer`,`num`)VALUES (NULL , '{$fromUsername}', '8','{$a}','0')";
      mysql_query($sql);
     } 
     else
     {
      $sql="UPDATE `menu2` SET `sec`='8',`answer`='{$a}', `num`='0' where `user`= '{$fromUsername}'";
      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'];
     $answer=explode(",",$answer);

     $num=$rm['num'];  
     if($sec=="8")
     {

      if($num=="0")
      {

       if($keyword==$array[$answer[0]])
       {
        $contentStr="恭喜你,答對了。\n第二題".$answer[1]."省會(huì)是哪里?";
        $sql="UPDATE `menu2` SET `num`='1' where `user`= '{$fromUsername}'";
        mysql_query($sql);
       }
       else
       {
        $contentStr="很遺憾,你錯(cuò)了,重新來玩吧。";
        $sql="UPDATE `menu2` SET `sec`='' where `user`= '{$fromUsername}'";
        mysql_query($sql);
       }
      } 
      elseif($num=="1")
      {
       if($keyword==$array[$answer[1]])
       {
        $contentStr="恭喜你,答對了。\n第三題".$answer[2]."省會(huì)是哪里?";
        $sql="UPDATE `menu2` SET `num`='2' where `user`= '{$fromUsername}'";
        mysql_query($sql);
       }
       else
       {
        $contentStr="很遺憾,再努力一把,重新來玩吧。";
        $sql="UPDATE `menu2` SET `sec`='' where `user`= '{$fromUsername}'";
        mysql_query($sql);
       }
      }
      elseif($num=="2")
      {
       if($keyword==$array[$answer[2]]){
       $contentStr="恭喜你,連闖三關(guān)!";
       $sql="UPDATE `menu2` SET `sec`='' where `user`= '{$fromUsername}'";
       mysql_query($sql);
      }
      else
      {
       $contentStr="很遺憾,就差一步,重新來玩吧。";
       $sql="UPDATE `menu2` SET `sec`='' where `user`= '{$fromUsername}'";
       mysql_query($sql);
      }
     }
    } 
    else 
    {
     $contentStr="請先輸入8";
    }
   }
   $msgType="text";
   $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
   echo $resultStr;

  }
 }

menu2.sql 數(shù)據(jù)庫代碼如下:

-- phpMyAdmin SQL Dump
-- version 3.3.8.1
-- http://www.phpmyadmin.net
--
-- 主機(jī): w.rdc.sae.sina.com.cn:3307
-- 生成日期: 2018 年 08 月 26 日 14:10
-- 服務(wù)器版本: 5.6.23
-- PHP 版本: 5.3.3

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- 數(shù)據(jù)庫: `app_dq095`
--

-- --------------------------------------------------------

--
-- 表的結(jié)構(gòu) `menu2`
--

CREATE TABLE IF NOT EXISTS `menu2` (
 `id` int(3) NOT NULL AUTO_INCREMENT,
 `user` varchar(255) NOT NULL,
 `sec` int(1) NOT NULL,
 `answer` varchar(10) NOT NULL,
 `num` int(4) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

--
-- 轉(zhuǎn)存表中的數(shù)據(jù) `menu2`
--

INSERT INTO `menu2` (`id`, `user`, `sec`, `answer`, `num`) VALUES
(1, 'ooD-ow-C8ZigAihscJN54pAWpu2g', 8, '廣東,江蘇,山東', 1),
(2, 'ooD-ow-C8ZigAihscJN54pAWpu2g', 8, '廣東,江蘇,山東', 1);

coon.php 連接數(shù)據(jù)庫代碼如下:

?php


  //用 戶 名 : $user
  //密  碼 : $pwd
  //主庫域名 : $host
  //從庫域名 : SAE_MYSQL_HOST_S
  //端  口 : $port
  //數(shù)據(jù)庫名 : $dbname

  $dbname = "app_dq095";
  $host = "w.rdc.sae.sina.com.cn";
  $port = "3306";
  $user = "4k514n103z";
  $pwd = "2402314li2j1i5im1xy2xizj5y332w2x41k2z203";

  /*接著調(diào)用mysql_connect()連接服務(wù)器*/
  // 連主庫
  $db = mysql_connect($host,$user,$pwd);

  if(!$db){
   die("Connect Server Failed: " . mysql_error($db));
  }

  /*連接成功后立即調(diào)用mysql_select_db()選中需要連接的數(shù)據(jù)庫*/
  if (!mysql_select_db($dbname)) {
   die("Select Database Failed: " . mysql_error($db));
  }
  mysql_query("set names utf-8",$db);

  /*至此連接已完全建立,就可對當(dāng)前數(shù)據(jù)庫進(jìn)行相應(yīng)的操作了*/
  /*!??!注意,無法再通過本次連接調(diào)用mysql_select_db來切換到其它數(shù)據(jù)庫了?。?!*/
  /* 需要再連接其它數(shù)據(jù)庫,請?jiān)偈褂胢ysql_connect+mysql_select_db啟動(dòng)另一個(gè)連接*/

  /**
    * 接下來就可以使用其它標(biāo)準(zhǔn)php mysql函數(shù)操作進(jìn)行數(shù)據(jù)庫操作
  */

index.php整體代碼如下:

?php
/**
 * wechat php test
 */


//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->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 = "xml>
        ToUserName>![CDATA[%s]]>/ToUserName>
        FromUserName>![CDATA[%s]]>/FromUserName>
        CreateTime>%s/CreateTime>
        MsgType>![CDATA[%s]]>/MsgType>
        Content>%s/Content>
        FuncFlag>0/FuncFlag>
       /xml>"; 
    include("coon.php");   
    $array=array("河北"=>"石家莊","山西"=>"太原","廣東"=>"廣州","黑龍江"=>"哈爾濱","廣西"=>"南寧","浙江"=>"杭州","江蘇"=>"南京","山東"=>"濟(jì)南","河南"=>"鄭州");
    if($keyword=="8")
    {
     $a=array_rand($array,3);
     $contentStr="第一題".$a[0]."省會(huì)是哪里?";
     $a=implode(",",$a); 
     $sql="SELECT * FROM `menu2` where `user`= '{$fromUsername}'";
     $query=mysql_query($sql);
     $rm=mysql_fetch_array($query); 
     $user=$rm['user'];
     if (empty($user))
     {
      $sql="INSERT INTO `menu2` (`id` ,`user` ,`sec`,`answer`,`num`)VALUES (NULL , '{$fromUsername}', '8','{$a}','0')";
      mysql_query($sql);
     } 
     else
     {
      $sql="UPDATE `menu2` SET `sec`='8',`answer`='{$a}', `num`='0' where `user`= '{$fromUsername}'";
      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'];
     $answer=explode(",",$answer);

     $num=$rm['num'];  
     if($sec=="8")
     {

      if($num=="0")
      {

       if($keyword==$array[$answer[0]])
       {
        $contentStr="恭喜你,答對了。\n第二題".$answer[1]."省會(huì)是哪里?";
        $sql="UPDATE `menu2` SET `num`='1' where `user`= '{$fromUsername}'";
        mysql_query($sql);
       }
       else
       {
        $contentStr="很遺憾,你錯(cuò)了,重新來玩吧。";
        $sql="UPDATE `menu2` SET `sec`='' where `user`= '{$fromUsername}'";
        mysql_query($sql);
       }
      } 
      elseif($num=="1")
      {
       if($keyword==$array[$answer[1]])
       {
        $contentStr="恭喜你,答對了。\n第三題".$answer[2]."省會(huì)是哪里?";
        $sql="UPDATE `menu2` SET `num`='2' where `user`= '{$fromUsername}'";
        mysql_query($sql);
       }
       else
       {
        $contentStr="很遺憾,再努力一把,重新來玩吧。";
        $sql="UPDATE `menu2` SET `sec`='' where `user`= '{$fromUsername}'";
        mysql_query($sql);
       }
      }
      elseif($num=="2")
      {
       if($keyword==$array[$answer[2]]){
       $contentStr="恭喜你,連闖三關(guān)!";
       $sql="UPDATE `menu2` SET `sec`='' where `user`= '{$fromUsername}'";
       mysql_query($sql);
      }
      else
      {
       $contentStr="很遺憾,就差一步,重新來玩吧。";
       $sql="UPDATE `menu2` SET `sec`='' where `user`= '{$fromUsername}'";
       mysql_query($sql);
      }
     }
    } 
    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;
  }
 }
}

?>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • php微信公眾號開發(fā)之簡答題

標(biāo)簽:安康 溫州 紹興 清遠(yuǎn) 萊蕪 呼倫貝爾 綏化 金華

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《php微信公眾號開發(fā)之答題連闖三關(guān)》,本文關(guān)鍵詞  php,微信,公眾,號開,發(fā)之,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《php微信公眾號開發(fā)之答題連闖三關(guān)》相關(guān)的同類信息!
  • 本頁收集關(guān)于php微信公眾號開發(fā)之答題連闖三關(guān)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章