Php 重建Github存储库树结构
借助PHP中的Github API,我试图在我的网页上显示所选回购协议的完整结构。结构指的是目录/子目录和相关文件:Php 重建Github存储库树结构,php,tree,github-api,Php,Tree,Github Api,借助PHP中的Github API,我试图在我的网页上显示所选回购协议的完整结构。结构指的是目录/子目录和相关文件: dir1 -subdir1-1 --file1 --file2 -subdir1-2 --subdir1-3 --file1 --file2 --file3 dir2 [...] 使用此端点,我可以获取所有必需的信息: https://api.github.com/repos/:owner/:repo-name/git/trees/HEAD:?recursive=1 例如,通过
dir1
-subdir1-1
--file1
--file2
-subdir1-2
--subdir1-3
--file1
--file2
--file3
dir2
[...]
使用此端点,我可以获取所有必需的信息:
https://api.github.com/repos/:owner/:repo-name/git/trees/HEAD:?recursive=1
例如,通过一次回购,我得到以下结果:
{
"sha": "d1dec6e8910b9e00370570caa5e5dedd930ebe9e",
"url": "https://api.github.com/repos/DivineOmega/DO-File-Cache/git/trees/d1dec6e8910b9e00370570caa5e5dedd930ebe9e",
"tree": [
{
"path": ".coveralls.yml",
"mode": "100644",
"type": "blob",
"sha": "dd9e7b806675d92983e7e3a1b504c18122b848c2",
"size": 106,
"url": "https://api.github.com/repos/DivineOmega/DO-File-Cache/git/blobs/dd9e7b806675d92983e7e3a1b504c18122b848c2"
},
{
"path": ".gitignore",
"mode": "100644",
"type": "blob",
"sha": "162bf51a335abe6a8905092f564fcafa154c197b",
"size": 39,
"url": "https://api.github.com/repos/DivineOmega/DO-File-Cache/git/blobs/162bf51a335abe6a8905092f564fcafa154c197b"
},
{
"path": ".travis.yml",
"mode": "100644",
"type": "blob",
"sha": "161ea29552f271918aaa24dfaa7f57c8746993aa",
"size": 269,
"url": "https://api.github.com/repos/DivineOmega/DO-File-Cache/git/blobs/161ea29552f271918aaa24dfaa7f57c8746993aa"
},
{
"path": "LICENSE",
"mode": "100644",
"type": "blob",
"sha": "341c30bda4457610db4829294e5d7e526d4c835b",
"size": 7652,
"url": "https://api.github.com/repos/DivineOmega/DO-File-Cache/git/blobs/341c30bda4457610db4829294e5d7e526d4c835b"
},
{
"path": "README.md",
"mode": "100644",
"type": "blob",
"sha": "f7b2da564b87dc7beaad260eb1c9674aae3161d8",
"size": 4760,
"url": "https://api.github.com/repos/DivineOmega/DO-File-Cache/git/blobs/f7b2da564b87dc7beaad260eb1c9674aae3161d8"
},
{
"path": "composer.json",
"mode": "100644",
"type": "blob",
"sha": "e83099ae43f1573493ff8fb967c0f8012f72dc0a",
"size": 859,
"url": "https://api.github.com/repos/DivineOmega/DO-File-Cache/git/blobs/e83099ae43f1573493ff8fb967c0f8012f72dc0a"
},
{
"path": "phpunit.xml",
"mode": "100644",
"type": "blob",
"sha": "0da3f08a6b4dd708f4c635d7647ab63c2926fb1f",
"size": 931,
"url": "https://api.github.com/repos/DivineOmega/DO-File-Cache/git/blobs/0da3f08a6b4dd708f4c635d7647ab63c2926fb1f"
},
{
"path": "src",
"mode": "040000",
"type": "tree",
"sha": "e1a3d6a8d9fa88e83a5b7e97c324abf205445dce",
"url": "https://api.github.com/repos/DivineOmega/DO-File-Cache/git/trees/e1a3d6a8d9fa88e83a5b7e97c324abf205445dce"
},
{
"path": "src/DOFileCache.php",
"mode": "100644",
"type": "blob",
"sha": "77d091424bd9cb62359339bc7bc68b6eacfe91c7",
"size": 8955,
"url": "https://api.github.com/repos/DivineOmega/DO-File-Cache/git/blobs/77d091424bd9cb62359339bc7bc68b6eacfe91c7"
},
{
"path": "tests",
"mode": "040000",
"type": "tree",
"sha": "19a801f7cdd01e9fb38d91a282a763b099590307",
"url": "https://api.github.com/repos/DivineOmega/DO-File-Cache/git/trees/19a801f7cdd01e9fb38d91a282a763b099590307"
},
{
"path": "tests/Functional",
"mode": "040000",
"type": "tree",
"sha": "65c076e8413e0a21876c3510e018f646b8712df9",
"url": "https://api.github.com/repos/DivineOmega/DO-File-Cache/git/trees/65c076e8413e0a21876c3510e018f646b8712df9"
},
{
"path": "tests/Functional/CacheStorageAndRetrievalTest.php",
"mode": "100644",
"type": "blob",
"sha": "09e7e3128dc316886ba19a14a4b4e0e43375dc78",
"size": 2713,
"url": "https://api.github.com/repos/DivineOmega/DO-File-Cache/git/blobs/09e7e3128dc316886ba19a14a4b4e0e43375dc78"
},
{
"path": "tests/Unit",
"mode": "040000",
"type": "tree",
"sha": "30d84ac9006176312c78787092618736d9258119",
"url": "https://api.github.com/repos/DivineOmega/DO-File-Cache/git/trees/30d84ac9006176312c78787092618736d9258119"
},
{
"path": "tests/Unit/.keep",
"mode": "100644",
"type": "blob",
"sha": "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391",
"size": 0,
"url": "https://api.github.com/repos/DivineOmega/DO-File-Cache/git/blobs/e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"
},
{
"path": "tests/Unit/BasicTest.php",
"mode": "100644",
"type": "blob",
"sha": "b459c228dab0d37f23bcaf20619eecfbacda6f42",
"size": 3933,
"url": "https://api.github.com/repos/DivineOmega/DO-File-Cache/git/blobs/b459c228dab0d37f23bcaf20619eecfbacda6f42"
}
],
"truncated": false
}
文件标记为“blob”,目录标记为“tree”type
。
我的问题来自路径信息。它显示对象的路径。如何递归地显示父目录中的每个对象(文件和子目录)
[编辑1]
我的实际PHP代码:
($i=0;$i<$count;$i++)的{
如果($trees['tree'][$i]['type']=='tree'){//directory
$dirs.=“文件夹”。$trees['tree'][$i]['path'].
;
}
如果($trees['tree'][$i]['type']=='blob'){//file
$files.='text_snippet'.$trees['tree'][$i]['path'].
;
}
}
echo$dirs$文件夹;
它只显示平面结构(顶层的dir和subdir),但我不知道如何实现所需的格式结构
谢谢你宝贵的帮助 向我们展示您迄今为止所做的尝试,以及您的最佳尝试(代码)。你可能比你想象的更接近解决方案。什么不按预期工作?请阅读!嗨,贝伦德,你说得对!我忘了嵌入我的代码。但我离结果还很远。在这里寻求帮助之前,我已经花了两天的时间进行搜索。