线上的接口的坑太多了。不如自己写一个吧。实际的业务场景中,可以用户回复关键词,可获取系统中相关的消息。
回复id号(自然数)或标题,返回相应的古诗。效果图如下:
在github上找到了一个古诗词数据——最全中华古诗词数据库, 唐宋两朝近一万四千古诗人, 接近5.5万首唐诗加26万宋诗. 两宋时期1564位词人,21050首词。
感谢数据来源贡献者。不过数据太大了(200M+)在此截取《诗经》305首作为基础数据。
源文件是json文件,在此通过navicat转换为shijing.sql文件导入到到线上数据库中。
数据款转换后存在一定的不良数据。
基本结构变成了m(数据模型-数据库)-v(视图)- c(控制逻辑)。
写一个getPoetry函数。
首先是配置环境变量,打印数据
private function getPoetry($object){
$word=trim($object->Content);
// 环境配置
$mysql_host='w.rdc.sae.sina.com.cn';
$mysql_port='3306';
$mysql_user='xolz3m1x2y';
$mysql_password='yw5lzlz0055kkk2l23xwihyxli04x41j5hz0zh2j';
$mysql_database='app_djtao';
$mysql_table='shijing';
$id=rand(1,305);
// 查询语句
$mysql_state = "SELECT * FROM `".$mysql_table."` WHERE `title` = '".$word."'";
$con=mysql_connect($mysql_host.':'.$mysql_port,$mysql_user,$mysql_password);
if(!$con){
die('Could not connect:'.mysql_error());
}
mysql_query("SET NAMES 'UTF-8'");
mysql_select_db($mysql_database,$con);
$result=mysql_query($mysql_state);
$content="";
while($row=mysql_fetch_array($result)){//将数据以数字索引方式储存在数组中
$content=$row;
break;
}
mysql_close($con);
$result=$this->transmitText($object,$content);
return $result;
}
加了一个不良数据处理。
private function transmitText($object,$content){
if(!isset($content)||empty($content)){
return "";
}
$xmlTpl="
<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>
";
$str=$content['content'];//优化处理
$str=str_replace('[','',$str);
$str=str_replace(']','',$str);
$str=str_replace('�','',$str);
$str=str_replace('"','',$str);
$str=str_replace(',','',$str);
$result=sprintf($xmlTpl,$object->FromUserName,$object->ToUserName,time(),$content['title'].'【'.$content['chapter'].'·'.$content['section'].'】'.' '.$str);
return $result;
}
由此,该需求顺利实现。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有