Skip to content

Commit 6d69335

Browse files
author
Taois
committed
add: 增加php自动配置
1 parent bcd8939 commit 6d69335

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

spider/php/config.php

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,24 @@
11
<?php
2-
// 设置返回为 JSON
3-
// http://127.0.0.1:9980/config.php
42
header('Content-Type: application/json; charset=utf-8');
5-
6-
// 当前目录
7-
$dir = __DIR__;
8-
9-
// 当前脚本名
3+
// http://127.0.0.1:9980/config.php
4+
// ==================
5+
// 1. 生成 sites
6+
// ==================
7+
$dir = __DIR__;
108
$self = basename(__FILE__);
11-
12-
// 扫描目录
139
$files = scandir($dir);
1410

1511
$sites = [];
1612

1713
foreach ($files as $file) {
18-
// 只处理 php 文件
1914
if (pathinfo($file, PATHINFO_EXTENSION) !== 'php') {
2015
continue;
2116
}
2217

23-
// 排除自身和 index.php
2418
if ($file === $self || $file === 'index.php') {
2519
continue;
2620
}
2721

28-
// 文件名(不含 .php)
2922
$filename = pathinfo($file, PATHINFO_FILENAME);
3023

3124
$sites[] = [
@@ -39,7 +32,31 @@
3932
];
4033
}
4134

42-
// 输出 JSON
35+
// ==================
36+
// 2. 尝试加载 ../drpy-node/index.json
37+
// ==================
38+
$indexJsonPath = realpath($dir . '/../drpy-node/index.json');
39+
40+
if ($indexJsonPath && is_file($indexJsonPath)) {
41+
$content = file_get_contents($indexJsonPath);
42+
$json = json_decode($content, true);
43+
44+
// JSON 合法并且是数组
45+
if (is_array($json)) {
46+
// 替换 sites
47+
$json['sites'] = $sites;
48+
49+
echo json_encode(
50+
$json,
51+
JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT
52+
);
53+
exit;
54+
}
55+
}
56+
57+
// ==================
58+
// 3. 找不到或失败,回退只返回 sites
59+
// ==================
4360
echo json_encode(
4461
["sites" => $sites],
4562
JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT

0 commit comments

Comments
 (0)