omines/akismet
Composer 安装命令:
composer require omines/akismet
包简介
Open source library for using the Wordpress Akismet anti spam service
README 文档
README
This library provides a straightforward object oriented and pluggable implementation of the well known online Akismet spam detection service. All known features and calls are implemented.
Documentation
Install the library with Composer:
composer require omines/akismet
Creating the service
You have to bring your own HTTP client implementation, based on Symfony Contracts. Easiest is to install the Symfony HTTP Client component:
composer require symfony/http-client
When using the Symfony framework this will provide a configurable service you can inject where needed, otherwise you can instantiate a stock client using:
$httpClient = HttpClient::create();
Now instantiate the service class, also providing your Akismet API key and the root URL of your site/blog:
$akismet = new Akismet($httpClient, 'Akismet API key', 'https://www.example.org/');
Using as a Symfony service
Add the following to config/services.yaml, assuming autowiring is enabled as it should in Symfony:
Omines\Akismet\Akismet: arguments: # Your 'blog', this should be the root URL of your deployment $instance: '%env(ROOT_URL)%' # Your Akismet API key $apiKey: '%env(AKISMET_KEY)%'
Then make sure your relevant .env file or the actual environment variables contain correct values for ROOT_URL
and AKISMET_KEY. You can now inject Omines\Akismet\Akismet wherever you need it.
Creating a message
You have to construct and fill an AkismetMessage before you can check or submit anything:
$message = (new AkismetMessage()) ->setUserIP('1.2.3.4') ->setType(MessageType::COMMENT) ->setContent('Some spammy message') ->setAuthorEmail('medicine_seller_1983@gmail.com') ;
Depending on your framework you will already have some useful data available in either a Symfony HTTP Foundation Request
instance or a PSR-7 ServerRequestInterface derivant. Bootstrapping a message from these to copy the user IP, user agent
and HTTP referrer fields is easy:
$message = AkismetMessage::fromRequest($symfonyRequest); // or $message = AkismetMessage::fromPSR7Request($psr7Request);
You can, and sometimes should, safely serialize the AkismetMessage class, for example when implementing moderation
queues. After moderation you can unserialize the message to submit the exact message you checked before as Ham or Spam.
Checking a message
Performing a spam check is then simple:
$response = $akismet->check($message); if ($response->isSpam()) { if ($response->shouldDiscard()) { // Akismet believes this to be the most pervasive and worst spam, not even worthy of moderation } else { // The message is considered spam, so it should either be refused or manually reviewed } }
Read their blog post on the 'should discard' feature.
Asynchronous invocation
When using symfony/http-client as the HTTP client implementation all calls are asynchronous out of the box. As such,
in the example above, the call to Akismet::check is not blocking further execution. This allows you to improve
your site performance by continuing to do other tasks in parallel, such as preparing notification emails and such.
Execution will block when you call any informational methods on the response, in this case the isSpam method call.
Submitting confirmed ham and spam
As mentioned before, when implementing moderation you can safely serialize the AkismetMessage instance submitted
for checking. After human review you can then help improve Akismet's services by submitting the message as ham or spam:
$response = $this->akismet->submitHam($message); $response = $this->akismet->submitSpam($message);
The MessageResponse object returned by these calls can, if you want, be checked for success with
$response->isSuccessful(), in an asynchronous method this is technically not required - the methods are fire and
forget.
Usage limit and activity
You can check your usage limits and key/site activity:
$response = $this->akismet->usageLimit(); if ($response->getPercentage() > 50) { @trigger_error('Used up more than half the usage limit', E_USER_WARNING); } $response = $this->akismet->activity(limit: 100); foreach ($response->getMonths() as $month => $activities) { // Do stuff }
For the parameters and response formats of the activity call refer to the Akismet documentation.
Contributing
Contributions are welcome and will be credited.
We accept contributions via Pull Requests on Github. Follow good standards, keep the PHPStan level maxed, and keep the test coverage at 100%.
Before committing, run bin/prepare-commit to automatically follow coding standards, run PHPStan and run all tests.
To run the live tests create a .env.local defining AKISMET_KEY. As they run in test mode they will not interfere
with your quota or get you banned.
Legal
This software was developed for internal use at Omines Full Service Internetbureau
in Eindhoven, the Netherlands. It is shared with the general public under the permissive MIT license, without
any guarantee of fitness for any particular purpose. Refer to the included LICENSE file for more details.
omines/akismet 适用场景与选型建议
omines/akismet 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.03k 次下载、GitHub Stars 达 11, 最近一次更新时间为 2023 年 04 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「Akismet」 「spam」 「antispam」 「spamfilter」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 omines/akismet 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 omines/akismet 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 omines/akismet 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Akismet antispam module for Silverstripe
Symfony Benji07 AkismetBundle
Simple captcha generator
reCAPTCHA library
Client library for reCAPTCHA with proxy support, a free service that protects websites from spam and abuse.
PHP library for implementing Akismet spam filtering
统计信息
- 总下载量: 5.03k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-04-19