fyrts / akismet
PHP library for implementing Akismet spam filtering
Requires
- php: ^7.1|^8.0
- guzzlehttp/guzzle: ~6.0|~7.0
Requires (Dev)
- phpunit/phpunit: ^8.4|^9.0
README
PHP client for the Akismet API. Akismet terms of use apply to usage of this library.
Installation
composer require fyrts/akismet
Usage
use Akismet\Akismet; use Akismet\Comment; // Instantiate API client with your API key and website root URL $akismet = new Akismet('api-key', 'https://www.example.org'); // Define content to check for spam $comment = new Comment(); $comment->setAuthor('Author Name') ->setAuthorEmail('author@example.org') ->setContent('Lorem ipsum dolor sit amet, consectetur adipiscing elit.'); // Optionally include additional environment variables for more accurate result $comment->includeServerVariables(); // Check for spam $result = $akismet->check($comment); if ($result->isSpam) { echo 'Comment is spam'; } if ($result->isDiscardable) { echo 'Comment is safe to discard'; }
It's possible to set Akismet parameters directly if preferred. A list of parameters is available at the official API documentation.
$comment = new Comment([ 'comment_author' => 'Author Name', 'comment_author_email' => 'author@example.org', 'comment_content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', ]); $comment->includeServerVariables();
If you'd like to provide all parameters by hand, you can just pass the array to the client directly.
$result = $akismet->check([ 'comment_author' => 'Author Name', 'comment_author_email' => 'author@example.org', 'comment_content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', ]);
Setting global parameters
For checking multiple comments, it's possible to set global language, charset and environment parameters for all requests.
$akismet->setLanguage('en'); $akismet->setCharset('UTF-8'); $akismet->includeServerVariables(); $result = $akismet->check($comment);
Available endpoints
The following endpoints are accessible through the Akismet\Akismet class:
Akismet::verifyKey()
Akismet::check($comment)
orAkismet::commentCheck($comment)
Akismet::submitSpam($comment)
Akismet::submitHam($comment)
Available comment parameter methods
The following methods are accessible through the Akismet\Comment class for setting parameters:
Comment::setUserIp($ip)
Comment::setUserAgent($user_agent)
Comment::setReferrer($referrer)
orComment::setReferer($referrer)
Comment::setPermalink($permalink)
Comment::setType($type)
orComment::setCommentType($type)
Comment::setAuthor($author)
orComment::setCommentAuthor($author)
Comment::setAuthorEmail($email)
orComment::setCommentAuthorEmail($email)
Comment::setAuthorUrl($url)
orComment::setCommentAuthorUrl($url)
Comment::setContent($content)
orComment::setCommentContent($content)
Comment::setDate($date)
orComment::setCommentDate($date)
Comment::setPostModifiedDate($date)
orComment::setCommentPostModifiedDate($date)
Comment::setBlogLanguage($language)
Comment::setBlogCharset($charset)
Comment::setUserRole($role)
Comment::setTest()
Comment::setRecheckReason($reason)
Any custom parameters can be set through Comment::setParameter($key, $value)
.
Date parameters accept DateTime objects, unix timestamps or date strings in ISO 8601 format.
License
fyrts/akismet
is licensed under the MIT License (MIT). Please see LICENSE for more information.