cms提供直接输出、PHP代码体两种方便开发者使用的数据输出接口。
![image image.png](https://file.xunruicms.com/vipfile/ueditor/image/201708/1503230943371177.png)
1、直接输出
本类型是将后台录入的数据格式原样输出到客户端
![image image.png](https://file.xunruicms.com/vipfile/ueditor/image/201708/1503232189884109.png)
返回格式为:
array(3) {
["code"]=>
int(1)
["msg"]=>
string(2) "ok"
["data"]=>
string(7) "test123"
}
2、JSON数组
本类型数据内容必须是一个json格式的数组字符串
![image image.png](https://file.xunruicms.com/vipfile/ueditor/image/201708/1503231325698419.png)
返回格式为:
array(3) {
["code"]=>
int(1)
["msg"]=>
string(2) "ok"
["data"]=>
array(3) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(1) "3"
}
}
3、php执行代码
本类型需要有php的开发经验,直接写php代码返回结果
例如:
定义test.php接口文件:config/myapi/test.php
<?php
/**
* api 示例文件
* 变量介绍
* $return 表示标准返回变量
*/
$return = []; // 返回数据
// 查询全部会员,并返回username和email
$data = \Phpcmf\Service::M()->table('member')->getAll();
if ($data) {
foreach ($data as $r) {
$return[] = [
'id' => $r['id'],
'username' => $r['username'],
];
}
}
后台选择test.php文件
![image image.png](https://file.xunruicms.com/vipfile/ueditor/image/201708/1503235994550105.png)
那么,返回格式为:
array(3) {
["code"]=>
int(1)
["msg"]=>
string(2) "ok"
["data"]=>
array(13) {
[0]=>
array(2) {
["id"]=>
string(1) "1"
["username"]=>
string(5) "admin"
}
......
本类型对php技能要求比较高,如果遇到执行错误可以在错误日志检查结果。
4、模板查询标签
本类型需要有CMS模板标签的使用经验,直接写标签代码返回结果
![image image.png](https://file.xunruicms.com/vipfile/ueditor/image/201709/1505350645342935.png)
例如调用news模块全部数据:
{module module=news}
或者
{list action=module module=news}
通过以上的list查询方法可调用循环标签的全部数据,当然你可以对结果进行格式化处理,使用回调方法。