## 调用API数据
- 一个百度搜索例子
#### 示例效果:路径/ht/baidu
![](/static/plugins/tphp/backstage/example/api/menu.png)
数据设置
```
<?php
/**
* 百度接口数据问题:
* 1、分页列数 15 错误, 分页大于 50 实则为 10
* 2、总页数错误,选择页数过大时数据和最后一页效果一样,也可能和第一页一样。
*/
$p = $_GET['p'];
empty($p) && $p = 1;
$psize = $_GET['psize'];
// 系统默认每页列数为20
empty($psize) && $psize = 20;
$wd = $_GET['title'];
// 默认搜索PHP,必须有个默认,否则无法使用百度接口
empty($wd) && $wd = 'php';
return [
'type' => 'api',
'config' => [
'url' => 'https://www.baidu.com/s?tn=json',
// GET 提交
'get' => [
// 'rn' => $psize, // 每页条数,参数rn值和参数psize一致,所以无需设置
'pn' => ($p - 1) * $psize, // 百度分页规则条数计算, 通常情况为: 'pn' => $p
'wd' => $wd
],
// 'post' => [], // POST提交
// 页头信息
'header' => [
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Encoding: gzip, deflate, br",
"Accept-Language: zh-CN,zh;q=0.9",
"Cache-Control: max-age=0",
"Connection: keep-alive",
"Sec-Fetch-Dest: document",
"Sec-Fetch-Mode: navigate",
"Sec-Fetch-Site: none",
"Sec-Fetch-User: ?1",
"Upgrade-Insecure-Requests: 1",
"User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36",
"Cookie: PSTM=1597733128; BAIDUID=5EF4A59E4BE333DE8AE51C8A7F6E6FCB:FG=1; BIDUPSID=958E25BA9D2F83AC45E99C1264812029; BD_UPN=123353; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; MCITY=-340%3A; BDSFRCVID=c3FOJeC62lKe3K7rh6IXtB6Y6t3FPljTH6f3FAxoaVFNakwdh1kvEG0PeU8g0KubhaS4ogKK3gOTH4PF_2uxOjjg8UtVJeC6EG0Ptf8g0M5; H_BDCLCKID_SF=tR30QbT256rDHJTg5DTjhPrMbp5CbMT-027OKKOO-RL-qRPCLpK5-pKPKP730xov2C6Holu-thF0hIt6Dj--jjOMMq8XbtQM-KkX3buQaDbP8pcNLTDKj-KQWGoybtkqLg5J5DQwJqnJ8xO33hO1j4_e5t5BWUcqtgck5fJJWx8KEl5jDh36XjksD-Rt5jc9tN6y0hvctb3cShPm0MjrDRLbXU6BK5vPbNcZ0l8K3l02V-bIe-t2b6QhDH0DJ6KJfRFsQ-F85njMHjrmbtOhq4tHeUOW0fRZ5mAqoD3lBJLaf4odj4TH5MjBWHj-JP0HyIQnaIQqaKOheU3jQ6o80MDd0qr-hP543bRTb-Py5KJvfj6n3tcbhP-UyNbMWh37JNRlMKoaMp78jR093JO4y4Ldj4oxJpOJ5JbMonLafD_MhDPRj5_aePk0MU_OqI62aKDs3KOIBhcqEIL4hMRqDUnWKxAqBlvJ0Jck_t-b3fJSjxbSj4D5Qx_bhUQp0f6K25vbBIjobq5nhMJN3j7JDMP0-x7KQ53y523iob3vQpPMVhQ3DRoWXPIqbN7P-p5Z5mAqKl0MLPbtbb0xXj_0-nDSHHD8q6JP; delPer=0; BD_CK_SAM=1; H_PS_PSSID=; PSINO=5; BDRCVFR[VjobkFsAYtR]=mk3SLVN4HKm; BDSVRTM=75",
],
// 分页信息
'page' => [
// params 参数在后台列表设置无效,因为后台分页强制为 p 和 psize
'params' => [
'page' => 'pn', // 当前页数, 默认: p
'pagesize' => 'rn', // 每页显示列数, 默认: psize
],
// info 数据返回制定分页信息
'info' => [
'page' => 'feed.pn', // 返回数据显示页数 默认: data.p
'pagesize' => 'feed.rn', // 返回数据显示每页列数 默认: data.psize
'total' => 'feed.all' // 总列表数 默认: data.total
]
],
// 列表数据下标
'list' => 'feed.entry',
// 提示信息下标
'msg' => 'feed.description',
// 状态下标, 如果不设置则不验证
'code' => false,
// 'code' => 'abc', 直接验证下标,并且 $data['abc'] == 1 时验证通过
// 'code' => ['abc', 200], 直接验证下标,并且 $data['abc'] == 200 时验证通过
],
'layout' => false
];
```
配置设置
```<?php
$title = $_GET['title'];
return [
'#SQL' => [
// 搜索title时默认为高亮替换
'title' => [
['set', '<a href="_[url]_" target="_blank">_[]_</a>']
],
// 描述高亮替换需手动设置
'abs' => [
['str_replace', $title, "<span style='color:#F33'>{$title}</span>"]
]
]
];
```
数据重设
```
<?php
return function ($data){
// dump($data); // 可打印或修改列表信息
return $data;
};
```
列表设置
```
<?php
return [
'field' => [
'title' => [
'name' => '标题',
'width' => 200,
'search' => [
'name' => '关键词',
'width' => 300
],
'length' => 1000 // 设置较大的字符长度,避免出现布局混乱,默认截取 200 字节长度
],
'abs' => [
'name' => '描述',
'width' => 300,
'length' => 1000 // 设置较大的字符长度,避免出现布局混乱,默认截取 200 字节长度
]
]
];
```
点击:后台模块 > 调用API数据
![](/static/plugins/tphp/backstage/example/api/demo.gif)
- 一个很方便的百度搜索出现了
- 同时也支持API接口数据的增删改,仅需要对传递的参数进行处理然后返回响应的值就行