В этой статье приведен пример того, как вы можете поделиться функцией второго уничтожения PHP Wechat для общего доступа к определенному коду, для справки, конкретное содержимое выглядит следующим образом
Небольшие точки знаний в базе данных:
Strtotime: Преобразование строк во время Time (): Функция времени, вызывающая текущее время системы
Основной код:
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $textTpl = ""; If ($keyword== "secondkill") { // User name: $user // Encryption: $pwd // Main Library Domain Name: $host // Slave Library Domain Name: SAE_MYSQL_HOST_S // End/mouth: $port // Database name: $dbname $dbname = "app_dq095"; $host = "w.rdc.sae.sina.com.cn"; $port = "3306"; $user = "4k514n103z"; $pwd = password; /* Then call mysql_connect() to connect to the server*/ // Connected main storehouse $db = mysql_connect($host,$user,$pwd); if(!$db){ die("Connect Server Failed: " . mysql_error($db)); } /* Call mysql_select_db() immediately after successful connection to select the database to connect to*/ if (!mysql_select_db($dbname)) { die("Select Database Failed: " . mysql_error($db)); } mysql_query("set names utf-8",$db); /* Now that the connection has been fully established, the current database can be operated accordingly.*/ /*!!! Note that mysql_select_db can no longer be called through this connection to switch to other databases!!! * / /* If you need to reconnect to another database, start another connection using mysql_connect+mysql_select_db*/ /** * Next you can use other standard PHP MySQL function operations for database operations */ $msgType = "text"; $shijian=strtotime("2018-08-23 23:00:00")-time(); if($shijian>0) { $contentStr = Sorry, the second kill hasn't started yet. It starts at 23:00 on August 23, 2018. }else{ $sql="SELECT * FROM `welcome`WHERE `user`= '" . iconv("UTF-8","GBK",$fromUsername) . "'"; $query=mysql_query($sql); $rs=mysql_fetch_array($query); $b= $rs['user']; $c=iconv("GBK","UTF-8",$b); if ($c==$fromUsername) { ContentStr = you've killed in seconds! }else{ $sql="SELECT `num` FROM `shuzi` WHERE 1"; $query=mysql_query($sql); $rs=mysql_fetch_array($query); $b= $rs['num']; if ($b>0) { ContentStr = Congratulations on your success in killing in seconds, with this tweet to our shop for 10 yuan in exchange for gifts. $b--; $sql="UPDATE `shuzi` SET `num`={$b} WHERE 1"; mysql_query($sql); }else{ ContentStr = Unfortunately, you didn't succeed in second killing. Come again next time. } $sqluser="INSERT INTO `welcome`(`id`,`user`) VALUES (NULL,'{$fromUsername}')"; mysql_query($sqluser); } } $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; mysql_close($db); }else{ echo "Input something..."; } %s 0
Код базы данных выглядит следующим образом:
-- phpMyAdmin SQL Dump -- version 3.3.8.1 -- http://www.phpmyadmin.net -- Host: w.rdc.sae.sina.com.cn:3307 - Generation date: 23 August 2018 22:29 Server version: 5.6.23 - PHP version: 5.3.3 SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; -- - Database: `app_dq095` -- -- -------------------------------------------------------- -- --The structure of the table `shuzizi'` -- CREATE TABLE IF NOT EXISTS `shuzi` ( `num` int(11) NOT NULL DEFAULT '0' ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- - Data `shuzi'in the dump table` -- INSERT INTO `shuzi` (`num`) VALUES (0); -- -------------------------------------------------------- -- --The structure of the table `welcome'` -- CREATE TABLE IF NOT EXISTS `welcome` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ; -- - Data `welcome'in the dump table` -- INSERT INTO `welcome` (`id`, `user`) VALUES (5, 'ooD-ow-C8ZigAihscJN54pAWpu2g');
В 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;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "
%s
0
";
If ($keyword== "secondkill")
{
// User name: $user
// Encryption: $pwd
// Main Library Domain Name: $host
// Slave Library Domain Name: SAE_MYSQL_HOST_S
// End/mouth: $port
// Database name: $dbname
$dbname = "app_dq095";
$host = "w.rdc.sae.sina.com.cn";
$port = "3306";
$user = "4k514n103z";
$pwd = "2402314li2j1i5im1xy2xizj5y332w2x41k2z203";
/* Then call mysql_connect() to connect to the server*/
// Connected main storehouse
$db = mysql_connect($host,$user,$pwd);
if(!$db){
die("Connect Server Failed: " . mysql_error($db));
}
/* Call mysql_select_db() immediately after successful connection to select the database to connect to*/
if (!mysql_select_db($dbname)) {
die("Select Database Failed: " . mysql_error($db));
}
mysql_query("set names utf-8",$db);
/* Now that the connection has been fully established, the current database can be operated accordingly.*/
/*!!! Note that mysql_select_db can no longer be called through this connection to switch to other databases!!! * /
/* If you need to reconnect to another database, start another connection using mysql_connect+mysql_select_db*/
/**
* Next you can use other standard PHP MySQL function operations for database operations
*/
$msgType = "text";
$shijian=strtotime("2018-08-23 23:00:00")-time();
if($shijian>0)
{
$contentStr = Sorry, the second kill hasn't started yet. It starts at 23:00 on August 23, 2018.
}else{
$sql="SELECT * FROM `welcome`WHERE `user`= '" . iconv("UTF-8","GBK",$fromUsername) . "'";
$query=mysql_query($sql);
$rs=mysql_fetch_array($query);
$b= $rs['user'];
$c=iconv("GBK","UTF-8",$b);
if ($c==$fromUsername)
{
ContentStr = you've killed in seconds!
}else{
$sql="SELECT `num` FROM `shuzi` WHERE 1";
$query=mysql_query($sql);
$rs=mysql_fetch_array($query);
$b= $rs['num'];
if ($b>0)
{
ContentStr = Congratulations on your success in killing in seconds, with this tweet to our shop for 10 yuan in exchange for gifts.
$b--;
$sql="UPDATE `shuzi` SET `num`={$b} WHERE 1";
mysql_query($sql);
}else{
ContentStr = Unfortunately, you didn't succeed in second killing. Come again next time.
}
$sqluser="INSERT INTO `welcome`(`id`,`user`) VALUES (NULL,'{$fromUsername}')";
mysql_query($sqluser);
}
}
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
mysql_close($db);
}else{
echo "Input something...";
}
}else {
echo "";
exit;
}
}
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;
}
}
}
?>Выше приведено все содержание этой статьи. Я надеюсь, что это будет полезно для изучения каждого, и я надеюсь, что вы будете больше поддерживать разработчика.