spatie / mixed-content-scanner
扫描您的网站以查找混合内容
Requires
- php: ^8.1
- spatie/crawler: ^8.0
Requires (Dev)
- phpunit/phpunit: ^10.0
- symfony/var-dumper: ^6.0
README
此包包含一个类,可用于扫描您的网站以查找混合内容。
以下是一个如何使用它的示例
use Spatie\MixedContentScanner\MixedContentScanner; $logger = new MixedContentLogger(); $scanner = new MixedContentScanner($logger); $scanner->scan('https://example.com');
MixedContentLogger
是一个类,其中包含在找到(或未找到)混合内容时被调用的方法。
如果您不需要自定义实现,而只想使用命令行工具查找混合内容,请查看我们的mixed-content-scanner-cli 包。
支持我们
通过观看我们的付费视频课程学习如何创建类似这样的包
我们投入了大量资源来创建最佳的开源包。您可以通过购买我们的付费产品之一来支持我们。
我们非常感谢您从家乡给我们寄明信片,并说明您正在使用我们哪些包。您可以在我们的联系页面上找到我们的地址。我们将所有收到的明信片发布在我们的虚拟明信片墙上。
安装
您可以通过 composer 安装此包
composer require spatie/mixed-content-scanner
内部工作原理
在扫描网站时,扫描器会爬取每一页。在检索到的 HTML 上,将检查以下元素和属性
audio
:src
embed
:src
form
:action
link
:href
iframe
:src
img
:src
,srcset
object
:data
param
:value
script
:src
source
:src
,srcset
video
:src
如果其中任何属性以 http://
开头,则该元素将被视为混合内容。
该包不会扫描链接的 .css
或 .js
文件,也不会考虑内联 <script>
或 <style>
和 短链接。
用法
use Spatie\MixedContentScanner\MixedContentScanner $logger = new MixedContentLogger(); $scanner = new MixedContentScanner($logger); $scanner->scan('https://example.com');
MixedContentScanner
接受一个扩展 \Spatie\MixedContentScannerMixedContentObserver
类的类的实例。您应该自己创建此类。让我们看看一个示例实现。
use Psr\Http\Message\UriInterface; use Spatie\MixedContentScanner\MixedContent; use Spatie\MixedContentScanner\MixedContentObserver; class MyMixedContentLogger extends MixedContentObserver { /** * Will be called when mixed content was found. * * @param \Spatie\MixedContentScanner\MixedContent $mixedContent */ public function mixedContentFound(MixedContent $mixedContent): void { } /** * Will be called when no mixed content was found on the given url. * * @param \Psr\Http\Message\UriInterface $crawledUrl */ public function noMixedContentFound(UriInterface $crawledUrl): void { } /** * Will be called when the scanner has finished crawling. */ public function finishedCrawling(): void { } }
当然,您应该为这些方法提供函数体。如果您不需要函数,只需将其删除即可。
mixedContentFound
类接受的 $mixedContent
变量是一个 \Spatie\MixedContentScanner\MixedContent
的实例,该实例具有以下三个属性
$elementName
: 被视为混合内容的元素的名称$mixedContentUrl
: 被视为混合内容的元素的 URL。对于图像,这可以是src
或srcset
的值;对于表单,这可以是action
的值等。$foundOnUrl
: 混合内容被找到的 URL
自定义请求
扫描器由我们自研的爬虫供电,该爬虫本身又利用Guzzle进行网络请求。您可以将选项数组传递给MixedContentScanner
的第二个参数。这些选项将传递给Guzzle客户端。
以下是一个关闭SSL验证的示例。
$scanner = new MixedContentScanner($logger); $scanner->scan('https://laravel.net.cn', ['verify' => 'false']);
过滤爬取的URL
默认情况下,混合内容扫描器会爬取给定主机的所有URL。如果您想过滤要爬取的URL,可以向扫描器传递一个扩展自Spatie\Crawler\CrawlProfile
的类。
以下是该类的代码
namespace Spatie\Crawler; use Psr\Http\Message\UriInterface; abstract class CrawlProfile { /** * Determine if the given url should be crawled. * * @param \Psr\Http\Message\UriInterface $url * * @return bool */ abstract public function shouldCrawl(UriInterface $url): bool; }
以下是让扫描器使用您配置文件的示例
use Spatie\MixedContentScanner\MixedContentScanner; $logger = new MixedContentLogger(); $scanner = new MixedContentScanner($logger); $scanner->setCrawlProfile(new MyCrawlProfile);
自定义爬虫
扫描器由我们的自研爬虫供电。您可以在开始爬取之前通过在MixedContentScanner
上调用configureCrawler
来调用爬虫上的任何方法。
use Spatie\Crawler\Crawler; use Spatie\MixedContentScanner\MixedContentScanner; $scanner = (new MixedContentScanner($logger)) ->configureCrawler(function(Crawler $crawler) { $crawler->setConcurrency(1) // now all urls will be crawled one by one });
变更日志
有关最近更改的更多信息,请参阅变更日志。
测试
composer test
贡献
有关详细信息,请参阅贡献指南。
安全
如果您发现有关安全性的bug,请通过security@spatie.be发送邮件,而不是使用问题跟踪器。
明信片软件
您可以使用此软件包,但如果它进入了您的生产环境,我们非常感谢您从您家乡寄给我们一张明信片,并注明您正在使用我们的哪个软件包。
我们的地址是:Spatie,Kruikstraat 22,2018 安特卫普,比利时。
我们将所有收到的明信片发布在我们的公司网站上。
鸣谢
扫描器受到Bram Van Damme的mixed-content-scan的启发。他的readme和代码的部分内容被使用。
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。