定制 gsteel/akismet 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

gsteel/akismet

Composer 安装命令:

composer require gsteel/akismet

包简介

Simple Akismet API Bindings

README 文档

README

Continuous Integration Psalm Type Coverage Latest Stable Version Total Downloads

Introduction

Provides a straight-forward way of using the Akismet anti-spam service in any-old PHP application.

Installation & Requirements

Requires PHP ~8.0 || ~8.1 || ~8.2

The library does not include an HTTP client, so if your project does not already have a PSR-18 HTTP Client installed, you will need to install one in order to use it. There are many HTTP clients to choose from, for example the popular libraries Guzzle or HttpPlug.

The library also requires that you have a PSR-17 (HTTP Factories) library installed, again, you can find implementations on Packagist and I personally favour laminas/laminas-diactoros.

composer require laminas/laminas-diactoros
composer require php-http/curl-client
composer require gsteel/akismet

Basic Usage

Construct a client

Once you have the requisite HTTP related libraries installed, they should become discoverable with the shipped discovery library. Provide an Api key, and the default website address that you will be operating with to the constructor, and you’ll have a ready-to-use client:

<?php

use GSteel\Akismet\Client;
use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;

$client = new Client(
    'myApiKey',
    'https://my-website.example.com',
    Psr18ClientDiscovery::find(),
    Psr17FactoryDiscovery::findRequestFactory(),
    Psr17FactoryDiscovery::findStreamFactory()
);

// Check the validity of your API key

$result = $client->verifyKey();
assert($result === true);

Comment Parameters & Checking Content

The primary concern of the library is to check requests such as comments and form submissions to ascertain whether the content is "Spam" or "Ham".

The Akismet API has a number of parameters available to improve the accuracy of the check which have been encapsulated into an immutable value object \Gsteel\Akismet\CommentParameters.

If you are familiar with the parameter names, you can pass in an array to the constructor of this object, otherwise you can call various methods to build an object to include all the information you wish to provide.

<?php

use GSteel\Akismet\AkismetClient;
use GSteel\Akismet\CommentParameters;
use GSteel\Akismet\CommentType;

assert($client instanceof AkismetClient);

$parameters = (new CommentParameters())
    ->withComment('Some comment Content', CommentType::contactForm())
    ->withRequestParams($_SERVER['REMOTE_ADDR']);
    
$result = $client->check($parameters);

assert($result->isSpam());

There are a considerable number of additional arguments and methods available that can be used to provide as much context as possible to the API, there is also a named constructor that is useful in an environment that utilises PSR-7 Messages:

<?php

use GSteel\Akismet\CommentParameters;
use Psr\Http\Message\ServerRequestInterface;

assert($request instanceof ServerRequestInterface);

$parameters = CommentParameters::fromRequest($request);

Submitting False Positives or False Negatives

Submitting content that was incorrectly classified as Spam can be achieved by sending the same payload used in check() to the method submitHam(). Conversely, you can submit incorrectly classified Ham as actual Spam with submitSpam():

<?php

use GSteel\Akismet\AkismetClient;
use GSteel\Akismet\Result;

assert($client instanceof AkismetClient);

$parameters = CommentParameters::fromRequest($request);
$result = $client->check($parameters);

// Result is incorrectly classified as spam:
$client->submitHam($result->parameters());

// Result is incorrectly classified as ham:
$client->submitSpam($result->parameters());

You can serialise and un-serialise a Result from the clients check() method to a JSON string:

<?php

use GSteel\Akismet\Result;

assert($result instanceof Result);

$jsonString = json_encode($result);

$hydratedResult = Result::fromJsonString($jsonString);

Coupled with your own method of identifying a specific request, you could create a mechanism to store the result in order to submit it as ham or spam based on user feedback.

Error Handling

All errors implement \GSteel\Akismet\GenericException so it is possible to catch any errors thrown by the library with a single type. Notable concrete exceptions include:

  • ApiError - Indicating that an api request failed because of an invalid key for example
  • HttpError - A failure to communicate with the Akismet via HTTP
  • InvalidRequestParameters - Required values missing, or invalid values given to CommentParameters

PSR-11 Containers

A factory is shipped in \GSteel\Akismet\Container\ClientDiscoveryFactory that can be wired up in your DI container of choice that leverages Http Discovery to inject Http clients and factories etc.

It assumes that the container will return an array or something implementing ArrayAccess by asking for config It further assumes that this configuration structure contains the following information:

<?php
$config = [
    'akismet' => [
        'key' => 'Your API Key',
        'website' => 'https://you.example.com',
    ],
];

License

Released under the MIT License - see the LICENSE file for details

gsteel/akismet 适用场景与选型建议

gsteel/akismet 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.61k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 08 月 31 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「Akismet」 「spam」 「API-Client」 「psr-17」 「psr-18」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 gsteel/akismet 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 gsteel/akismet 我们能提供哪些服务?
定制开发 / 二次开发

基于 gsteel/akismet 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 8.61k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-08-31