本文實例為大家分享了php微信公眾號開發簡答題的具體代碼,供大家參考,具體內容如下
簡答題
- array("第5頁 第1個字" => "王","第8頁 第1個字" => "李","第30頁 第1個字" => "周",)
- array_rand($array,1)
- 數據庫建立二級菜單


核心代碼如下:
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");
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:連接數據庫代碼如下:
?php
//用 戶 名 : $user
//密 碼 : $pwd
//主庫域名 : $host
//從庫域名 : SAE_MYSQL_HOST_S
//端 口 : $port
//數據庫名 : $dbname
$dbname = "app_dq095";
$host = "w.rdc.sae.sina.com.cn";
$port = "3306";
$user = "4k514n103z";
$pwd = "2402314li2j1i5im1xy2xizj5y332w2x41k2z203";
/*接著調用mysql_connect()連接服務器*/
// 連主庫
$db = mysql_connect($host,$user,$pwd);
if(!$db){
die("Connect Server Failed: " . mysql_error($db));
}
/*連接成功后立即調用mysql_select_db()選中需要連接的數據庫*/
if (!mysql_select_db($dbname)) {
die("Select Database Failed: " . mysql_error($db));
}
mysql_query("set names utf-8",$db);
/*至此連接已完全建立,就可對當前數據庫進行相應的操作了*/
/*!!!注意,無法再通過本次連接調用mysql_select_db來切換到其它數據庫了!!!*/
/* 需要再連接其它數據庫,請再使用mysql_connect+mysql_select_db啟動另一個連接*/
/**
* 接下來就可以使用其它標準php mysql函數操作進行數據庫操作
*/
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");
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;
}
}
}
?>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。