笑话
笑话是人生之所需,是健康生活的调味品、维生素,广泛存在于社会,无处不在。凡是有人群的地方,都离不开它。
数据获取
目前笑话主要存在于一些门户或者专业的网站,可以说使用爬虫程序将其采集下来,然后保存在本地数据库中。
数据库脚本SQL如下:
--
-- 表的结构 `joke`
--
DROP TABLE IF EXISTS `joke`;
CREATE TABLE IF NOT EXISTS `joke` (
`id` int(5) NOT NULL AUTO_INCREMENT,
`content` varchar(600) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk AUTO_INCREMENT=1001 ;
id :表示序号
content:为笑话内容
INSERT INTO `joke` (`id`, `content`) VALUES
(1, '她:“因为别人都不同情你,我才做你的妻子。”他:“你总算成功了。现在每个人都因此同情我。”'),
(2, '女:“为什么从前你对我百依百顺,可结婚才三天,你就跟我吵了两天的架?”男:“因为我的忍耐是有限度的。”'),
(3, '燕尔新婚,新娘对新郎说:“今后咱们不兴说‘我的’了,要说‘我们的’。”新郎去洗澡,良久不出,新娘问:“你在干什么哪?”“亲爱的,我在刮我们的胡子呢。”'),
导入数据库,形成一个小型笑话库。
开发实现
在数据库中查询笑话内容的代码如下:
<?php
function getJokeInfo()
{
$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_table = "joke";
$id = rand(1, 1000);
$mysql_state = "SELECT * FROM `".$mysql_table."` WHERE `id` = '".$id."'";
$result = mysql_query($mysql_state);
$joke = "";
while($row = mysql_fetch_array($result))
{
$joke = $row["content"];
break;
}
mysql_close($con);
return $joke;
}
?>
$id = rand(1, 1000);:从表“joke”中随机查询一条记录
index.php整体代码如下
<?php
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
if (!isset($_GET['echostr'])) {
$wechatObj->responseMsg();
}else{
$wechatObj->valid();
}
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
echo $echoStr;
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;
}
}
public function responseMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$RX_TYPE = trim($postObj->MsgType);
switch ($RX_TYPE)
{
case "text":
$resultStr = $this->receiveText($postObj);
break;
case "event":
$resultStr = $this->receiveEvent($postObj);
break;
default:
$resultStr = "";
break;
}
echo $resultStr;
}else {
echo "";
exit;
}
}
private function receiveText($object)
{
$funcFlag = 0;
$keyword = trim($object->Content);
$resultStr = "";
$contentStr = "";
include("joke.php");
$contentStr = getJokeInfo();
$resultStr = $this->transmitText($object, trim($contentStr), $funcFlag);
return $resultStr;
}
private function receiveEvent($object)
{
$contentStr = "";
switch ($object->Event)
{
case "subscribe":
$contentStr = "欢迎关注 德强1012";
}
$resultStr = $this->transmitText($object, $contentStr);
return $resultStr;
}
private function transmitText($object, $content, $flag = 0)
{
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>%d</FuncFlag>
</xml>";
$resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag);
return $resultStr;
}
}
?>