如何编写Discuz论坛返回JSON数据的接口

 我来答
EvenHHZ
2016-10-17 · 知道合伙人软件行家
EvenHHZ
知道合伙人软件行家
采纳数:13691 获赞数:18845
个人出版图书:《玩转Python网络爬虫》、《玩转Django2.0》

向TA提问 私信TA
展开全部
<?php header('Content-type:text/json;charset=utf-8');
define('APPTYPEID', 5);
define('CURSCRIPT', 'userapp');
require_once './source/class/class_core.php';
$discuz = &discuz_core::instance();
$discuz -> cachelist = $cachelist;
$discuz -> init();

try {
if ($_GET["type"] == "wdnew") {
$query = DB::query("SELECT * FROM " . DB::table('forum_thread') . " WHERE fid=49 ORDER BY dateline ASC limit 10");
$jsonstr = "{state:10000,data:[";
while ($soft = DB::fetch($query)) {
$newlist .= "{\"id\":" . $soft['tid'] . ",\"title\":\"" . $soft['subject'] . "\"},";
}
$jsonstr = $jsonstr . rtrim($newlist, ',') . "]}";
echo $jsonstr;
} else if ($_GET["type"] == "getForumClass") {
$query = DB::query("SELECT * FROM " . DB::table('forum_forum') . " WHERE status=1 ORDER BY fid ASC");
$jsonstr = "{state:10000,data:[";
while ($soft = DB::fetch($query)) {
$newlist .= "{\"id\":" . $soft['fid'] . ",\"fup\":" . $soft['fup'] . ",\"name\":\"" . $soft['name'] . "\",\"px\":" . $soft['displayorder'] . "},";
}
$jsonstr = $jsonstr . rtrim($newlist, ',') . "]}";
echo $jsonstr;
} else if ($_GET["type"] == "getForumInfo") {
$fid = $_GET["fid"];
$dateline_min = $_GET["dateline_min"];
$dateline_max = $_GET["dateline_max"];
$datacount = DB::result_first("SELECT COUNT(*) FROM " . DB::table('forum_thread') . " WHERE fid=".$fid." and closed=0 and dateline>=".$dateline_min." and dateline<=".$dateline_max);
if($datacount==0){
$jsonstr = "{state:10001,data:[]}";
}else{
$query = DB::query("SELECT * FROM " . DB::table('forum_thread') . " WHERE fid=".$fid." and closed=0 and dateline>=".$dateline_min." and dateline<=".$dateline_max);
$jsonstr = "{state:10000,data:[";
while ($soft = DB::fetch($query)) {
$typename = DB::result_first("SELECT name FROM " . DB::table('forum_threadclass') . " WHERE typeid=".$soft['typeid'] );
$newlist .= "{\"id\":" . $soft['tid'] . ",\"title\":\"[".$typename."]" . $soft['subject'] . "\",\"price\":\"" . $soft['price'] . "\",\"views\":\"" . $soft['views'] . "\",\"dateline\":\"" . $soft['dateline'] . "\"},";
}
$jsonstr = $jsonstr . rtrim($newlist, ',') . "]}";
}
echo $jsonstr;
}else {
echo "{state:0,data:[]}";
}
} catch(Exception $e) {
echo "{state:0,data:[]}";
}
?>
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式