MicroAkismet

By Gaby Vanhegan, 20070115

A straightforward set of functions for talking to the Akismet blog-spam protection system. This is intended as a simple and compact method for adding Akismet protection to any user-submitted content. This script conforms to the information on the Akismet API page:

http://akismet.com/development/api/

There are two versions, a bunch of static functions or a stand-alone class. Usage is practically identical for either, you can use whichever one you want.

Downloads

Current Version: microakismet-1.2.zip

MD5 (microakismet-1.2.zip) = fd0581c5ef2c81a23e50af4005c0ef62

README.txt

LICENSE.txt

Installation

1. To begin using the functions, download the latest version and unzip the script.

2. Place the php file you want somewhere in your web root, and load the functions or class using:

<?php 

include_once( "func.microakismet.inc.php" );

?>

Or:

<?php 

include_once( "class.microakismet.inc.php" );

?>

3. Obtain a valid WordPress API key, if you do not already have one, from:

http://wordpress.com/

If you don't have an account you will need to create one. Once you have an account, your API key can be found at the bottom of this page:

http://wordpress.com/profile

4a. If you're using the static functions (func.microakismet.inc.php) then you will need to set the global variables at the top of the file; your WordPress API key and the home page of the blog being protected. The User Agent can be changed to suit your application. If it is not a blog that is being protected, change the home page to the comments page or forum page that is being protected.

4b. If you are using the class file (func.microakismet.inc.php), the API key, homepage and user agent are specified in the class constructor.

Usage

There are three static functions for talking to Akismet. In the static functions:

And the corresponding member functions in the class version:

Each function takes one argument, $vars, which is a list of information about the comment that is being checked. $vars *must* contain at least this information

The "blog" value (the homepage of the blog that this post came from) is added automatically by the code. The following extra information can also be added, to help Akismet classify the message more accurately:

In PHP, you can also add the contents of the $_SERVER array to this list, which gives extra information to Akismet, and further increases the accuracy of their system.

Example

To check if a message is spam or not:

<?php

   // The array of data we need
   $vars    = array();

   // Add the contents of the $_SERVER array, to help Akismet out
   foreach ( $_SERVER as $key => $val ) { $vars[ $key ] = $val; }

   // Mandatory fields of information
   $vars["user_ip"]              = $_SERVER["REMOTE_ADDR"];
   $vars["user_agent"]           = $_SERVER["HTTP_USER_AGENT"];

   // The body of the message to check, the name of the person who
   // posted it, and their email address
   $vars["comment_content"]      = $_POST["comment"];
   $vars["comment_author"]       = $_POST["sender_name"];
   $vars["comment_author_email"] = $_POST["sender_email"];

   // ... Add more fields if you want

   // Check if it's spam
   if ( akismet_check( $vars ) ) {
      die( "The message was spam!" );
   }
   else {
      // ...
      // Do whatever we do if the message was OK
   }

?>

Or to do this using the class version:

<?php

   // The array of data we need
   $vars    = array();

   // ... Add vars as before ...
   
   // Create a MicroAkismet object with our details in it
   $akismet = new MicroAkismet(  "your.wordpress.api.key",
                                 "http://homepaeg.com/blog/comments",
                                 "mysite.com/1.0" );

   // Check if it's spam
   if ( $akismet->check( $vars ) ) {
      die( "The message was spam!" );
   }
   else {
      // Do whatever we do if the message was OK
   }

?>

Reclassifying a message as spam or ham uses exactly the same process, but you must make sure that:

  1. You stored a copy of the poster's user-agent and IP address with the posting.
  2. You stored a copy of $_SERVER array that was present at the time the post was made. You can use the php function serialize() to convert it into single string, which is easier to store in a database and pull out later. When you need it later, you can unserialize the string and it comes back out as an array, which you can then use in the above example.