Prevent comment spam using Akismet service, in PHP.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.vscode
doc
example
lib
test
var
.editorconfig
.gitattributes
.gitignore
.semver
.travis.yml
AUTHORS.txt
CHANGELOG.md
LICENSE.md
README.md
RoboFile.php
composer.json
mkdocs.yml
phpdoc.xml
phpstan.neon
phpunit.xml

README.md

Akismet for PHP

Runtime Release License Downloads Coverage Build

Prevent comment spam using Akismet service, in PHP.

Resources

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::CONTACT_FORM
  );

  $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\Client class is an EventEmitter that triggers some events during its life cycle:

  • request : emitted every time a request is made to the remote service.
  • response : emitted every time a response is received from the remote service.

You can subscribe to them using the on() method:

<?php
use Psr\Http\Message\{RequestInterface, ResponseInterface};

$client->on(Client::EVENT_REQUEST, function(RequestInterface $request) {
  echo 'Client request: ', $request->getUri();
});

$client->on(Client::EVENT_RESPONSE, function($request, ResponseInterface $response) {
  echo 'Server response: ', $response->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 test

License

Akismet for PHP is distributed under the MIT License.