Prevent comment spam using Akismet service, for the Yii Framework. https://dev.belin.io/yii2-akismet
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 0843999779 Code optimization 1 day ago
.github Added a funding file 2 months ago
.vscode Updated the VSCode tasks 6 months ago
doc Updated the documentation 2 weeks ago
etc Updated the PHPDoc settings 3 weeks ago
lib Replaced some late static bindings by `self` 3 weeks ago
share Updated the URL of the default endpoint 4 months ago
test Updated the tests 3 weeks ago
var Initial commit 2 years ago
.editorconfig Updated a link 11 months ago
.gitattributes Updated the Git attributes 2 months ago
.gitignore Added a funding file 2 months ago
.semver Bumped the version number 4 months ago
.travis.yml Updated the CI 2 months ago
AUTHORS.txt Added the legal files 2 years ago
CHANGELOG.md Using camelcase instead of all caps for constants 4 months ago
LICENSE.md Updated the copyright notice 7 months ago
README.md Updated the development dependencies 2 months ago
RoboFile.php Code optimization 1 day ago
composer.json Updated the dependencies 3 days ago

README.md

Akismet for Yii

PHP Yii Framework Release License Downloads Coverage Build

Prevent comment spam using the Akismet connector for Yii, high-performance PHP framework.

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

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 via Composer

From a command prompt, run:

composer require cedx/yii2-akismet

Usage

In your application configuration file, you can use the following component:

<?php return [
  'components' => [
    'akismet' => [
      'class' => 'yii\akismet\http\Client',
      'apiKey' => '123YourAPIKey',
      'blog' => [
        'class' => 'yii\akismet\Blog',
        'url' => 'http://www.yourblog.com'
      ]
    ]
  ]
];

Once the yii\akismet\http\Client component initialized with your credentials, you can use its methods.

Key verification

<?php
use yii\akismet\http\{ClientException};

try {
  $client = \Yii::$app->akismet;
  $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 yii\akismet\{Author, Comment};

try {
  $comment = new Comment(
    new Author('127.0.0.1', 'Mozilla/5.0'),
    ['content' => 'A user comment', 'date' => new \DateTime]
  );

  $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 yii\akismet\http\Client class triggers some events during its life cycle.

The Client::eventRequest event

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

<?php
use yii\akismet\http\{Client};
use yii\httpclient\{RequestEvent};

$client->on(Client::eventRequest, function(RequestEvent $event) {
  echo 'Client request: ', $event->request->url;
});

The Client::eventResponse event

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

<?php
use yii\akismet\http\{Client};
use yii\httpclient\{RequestEvent};

$client->on(Client::eventResponse, function(RequestEvent $event) {
  echo 'Server response: ', $event->response->statusCode;
});

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

Resources

License

Akismet for Yii is distributed under the MIT License.