glicer/link-checker

检查 html 文件、sitemap.xml 和 robots.txt 中的断链

dev-master 2021-01-19 18:14 UTC

This package is not auto-updated.

Last update: 2024-09-25 05:46:42 UTC


README

Scrutinizer Code Quality Build Status Coverage Status SensioLabsInsight

检查 html/json 文件、sitemap.xml、markdown 和 robots.txt 中的断链。

它支持使用

安装

此库可在 Packagist 上找到。

推荐的安装方法是使用 composer

编辑你的 composer.json 并添加

{
    "require": {
       "glicer/link-checker": "dev-master"
    }
}

安装依赖

php composer.phar install

如何检查 html/json 文件中的链接?

require 'vendor/autoload.php';

use GlLinkChecker\GlLinkChecker;
use GlLinkChecker\GlLinkCheckerReport;
use Symfony\Component\Finder\Finder;

//relative url use host http://lyon.glicer.com to check link
$linkChecker  = new GlLinkChecker('http://lyon.glicer.com');

//construct list of local html and json files to check
$finder = new Finder();
$files  = $finder->files()->in('./public')->name("*.html")->name("*.json");

//launch links checking
$result  = $linkChecker->checkFiles(
    $files,
    function ($nbr) {
        // called at beginning - $nbr urls to check
    },
    function ($url, $files) {
        // called each $url - $files : list of filename containing $url link
    },
    function () {
        // called at the end
    }
);

//convert $result array in a temp html file
$filereport = GlLinkCheckerReport::toTmpHtml('lyonCheck',$result);

//$filereport contain fullpath to html file
print_r($filereport);

你可以用浏览器查看 $filereport

如何检查 robots.txt 和 sitemap 文件中的链接?

require 'vendor/autoload.php';

use GlLinkChecker\GlLinkChecker;

$linkChecker = new GlLinkChecker('http://lyon.glicer.com');
$result      = $linkChecker->checkRobotsSitemap();

print_r($result);

GlLinkChecker::checkRobotsSitemap() 返回一个数组,如下所示

$result = [
    'disallow' =>
        ['error' => ['/img/', '/download/']],
    'sitemap'  =>
        [
            'ok' => [
                '/sitemap.xml' =>
                    [
                        'ok' =>
                            [
                                '/index.html',
                                '/section/probleme-solution/compresser-css-html-js.html'
                            ]
                    ]
            ]
        ]
];

运行测试

从命令行启动

vendor\bin\phpunit

许可证 MIT

联系

作者:Emmanuel ROECKER & Rym BOUCHAGOUR

Web 开发博客 - http://dev.glicer.com