Download the PHP package omines/akismet without Composer
On this page you can find all versions of the php package omines/akismet. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package akismet
Akismet Client Library for PHP
This library provides a straightforward object oriented and pluggable implementation of the well known online Akismet spam detection service. All known features and calls are implemented.
Documentation
Install the library with Composer:
Creating the service
You have to bring your own HTTP client implementation, based on Symfony Contracts. Easiest is to install the Symfony HTTP Client component:
When using the Symfony framework this will provide a configurable service you can inject where needed, otherwise you can instantiate a stock client using:
Now instantiate the service class, also providing your Akismet API key and the root URL of your site/blog:
Using as a Symfony service
Add the following to config/services.yaml
, assuming autowiring is enabled as it should in Symfony:
Then make sure your relevant .env
file or the actual environment variables contain correct values for ROOT_URL
and AKISMET_KEY
. You can now inject Omines\Akismet\Akismet
wherever you need it.
Creating a message
You have to construct and fill an AkismetMessage
before you can check or submit anything:
Depending on your framework you will already have some useful data available in either a Symfony HTTP Foundation Request
instance or a PSR-7 ServerRequestInterface
derivant. Bootstrapping a message from these to copy the user IP, user agent
and HTTP referrer fields is easy:
You can, and sometimes should, safely serialize the AkismetMessage
class, for example when implementing moderation
queues. After moderation you can unserialize the message to submit the exact message you checked before as Ham or Spam.
Checking a message
Performing a spam check is then simple:
Read their blog post on the 'should discard' feature.
Asynchronous invocation
When using symfony/http-client
as the HTTP client implementation all calls are asynchronous out of the box. As such,
in the example above, the call to Akismet::check
is not blocking further execution. This allows you to improve
your site performance by continuing to do other tasks in parallel, such as preparing notification emails and such.
Execution will block when you call any informational methods on the response, in this case the isSpam
method call.
Submitting confirmed ham and spam
As mentioned before, when implementing moderation you can safely serialize the AkismetMessage
instance submitted
for checking. After human review you can then help improve Akismet's services by submitting the message as ham or spam:
The MessageResponse
object returned by these calls can, if you want, be checked for success with
$response->isSuccessful()
, in an asynchronous method this is technically not required - the methods are fire and
forget.
Usage limit and activity
You can check your usage limits and key/site activity:
For the parameters and response formats of the activity call refer to the Akismet documentation.
Contributing
Contributions are welcome and will be credited.
We accept contributions via Pull Requests on Github. Follow good standards, keep the PHPStan level maxed, and keep the test coverage at 100%.
Before committing, run bin/prepare-commit
to automatically follow coding standards, run PHPStan and run all tests.
To run the live tests create a .env.local
defining AKISMET_KEY
. As they run in test mode they will not interfere
with your quota or get you banned.
Legal
This software was developed for internal use at Omines Full Service Internetbureau
in Eindhoven, the Netherlands. It is shared with the general public under the permissive MIT license, without
any guarantee of fitness for any particular purpose. Refer to the included LICENSE
file for more details.