Prevent comment spam using Akismet service, in PHP. https://dev.belin.io/akismet.php
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Cédric Belin 060e425513 Updated the static analyzer 3 days ago
.github Renamed the GitHub workflow 4 weeks ago
.vscode Updated the VSCode tasks 3 weeks ago
doc Renamed the GitHub workflow 4 weeks ago
etc Enabled the TestDox reporting 1 week ago
example Using camelcase instead of all caps for constants 6 months ago
lib Updated the documentation 1 month ago
share Updated the URL of the default endpoint 7 months ago
test Using TestDox annotations 1 week ago
var Initial commit 3 years ago
.editorconfig Updated a link 1 year ago
.gitattributes Updated the Git attributes 4 months ago
.gitignore Added a funding file 4 months ago
.semver Bumped the version number 7 months ago
AUTHORS.txt Added the legal files 3 years ago
CHANGELOG.md Updated the URL of the PHP website 2 months ago
LICENSE.md Updated the copyright notice 9 months ago
README.md Renamed the GitHub workflow 4 weeks ago
RoboFile.php Moved the MkDocs settings to the "doc" folder 1 month ago
composer.json Updated the static analyzer 3 days ago

README.md

Akismet for PHP

Runtime Release License Downloads Coverage Build

Prevent comment spam using Akismet service, in PHP.

Documentation

Development

Features

  • Key verification: checks an Akismet API key and gets a value indicating whether it is valid.
  • Comment check: checks a comment and gets a value indicating whether it is spam.
  • Submit spam: submits a comment that was not marked as spam but should have been.
  • Submit ham: submits a comment that was incorrectly marked as spam but should not have been.

Requirements

You need the latest PHP and Composer versions to use the Akismet library.

If you plan to play with the sources, you will also need the latest Robo and Material for MkDocs versions.

Installing with Composer package manager

From a command prompt, run:

composer require cedx/akismet

Usage

Key verification

<?php
use Akismet\{Blog, Client, ClientException};
use GuzzleHttp\Psr7\{Uri};

try {
  $client = new Client('123YourAPIKey', new Blog(new Uri('https://www.yourblog.com')));
  $isValid = $client->verifyKey();
  echo $isValid ? 'The API key is valid' : 'The API key is invalid';
}

catch (ClientException $e) {
  echo 'An error occurred: ', $e->getMessage();
}

Comment check

<?php
use Akismet\{Author, Comment, CommentType};

try {
  $comment = new Comment(
    new Author('127.0.0.1', 'Mozilla/5.0'),
    'A user comment',
    CommentType::contactForm
  );

  $isSpam = $client->checkComment($comment);
  echo $isSpam ? 'The comment is spam' : 'The comment is ham';
}

catch (ClientException $e) {
  echo 'An error occurred: ', $e->getMessage();
}

Submit spam / ham

<?php
try {
  $client->submitSpam($comment);
  echo 'Spam submitted';

  $client->submitHam($comment);
  echo 'Ham submitted';
}

catch (ClientException $e) {
  echo 'An error occurred: ', $e->getMessage();
}

Events

The Akismet\Http\Client class is a League\Event\Emitter that triggers some events during its life cycle.

The Client::eventRequest event

Emitted every time a request is made to the remote service:

<?php
use Akismet\{Blog};
use Akismet\Http\{Client, RequestEvent};
use GuzzleHttp\Psr7\{Uri};

function main(): void {
  $client = new Client('123YourAPIKey', new Blog(new Uri('https://www.yourblog.com')));
  $client->addListener(Client::eventRequest, function(RequestEvent $event) {
    echo 'Client request: ', $event->getRequest()->getUri();
  });
}

The Client::eventResponse event

Emitted every time a response is received from the remote service:

<?php
use Akismet\{Blog};
use Akismet\Http\{Client, ResponseEvent};
use GuzzleHttp\Psr7\{Uri};

function main(): void {
  $client = new Client('123YourAPIKey', new Blog(new Uri('https://www.yourblog.com')));
  $client->addListener(Client::eventResponse, function(ResponseEvent $event) {
    echo 'Server response: ', $event->getResponse()->getStatusCode();
  });
}

Unit tests

In order to run the tests, you must set the AKISMET_API_KEY environment variable to the value of your Akismet API key:

export AKISMET_API_KEY="<123YourAPIKey>"

Then, you can run the test script from the command prompt:

composer run-script test

License

Akismet for PHP is distributed under the MIT License.