Inspired by Akismet is my Hero on blog.phpdeveloper.org and by the fact that Akismet has already stopped several thousand spam comments on my own WordPress blog, I decided to put together an Akismet class using the Zend Framework (resulting in this being PHP 5 only). For those of you who don’t know, Akismet is a web service that you can send comments to and they’ll let you know whether or not they think it’s spam. It does a great job of spotting spam and was a great addition to WordPress.

This class, which you can download along with api docs uses the Zend Framework’s Zend_Http_Client functionality to perform HTTP posts to Akismet and get the responses back. More specifically, it uses the version of Zend_Http_Client that’s currently in the incubator, not the one in the regular library.

Here’s a simple usage example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$api_key = 'your api key here';
$website_front_page = 'http://www.gregphoto.net';
$message = array(
	'comment_author' => 'viagra-test-123',
	'comment_author_email' => 'viagra-test-123@hotmails.com',
	'comment_author_url' => 'http://www.viagra-test-123.com/ilovespam/',
	'comment_content' => "Please buy my spam.  Mechanically separated meat tastes great!"
);
 
$akismet = new Gregphoto_Service_Akismet($api_key,$website_front_page);
if($akismet->checkIsSpam($message)) {
	echo 'it is spam!';
} else {
	echo 'it is not spam!!';
}

In addition to checking whether a message is spam (Gregphoto_Service_Akismet::checkIsSpam), the following methods are provided, covering the entirety of the Akismet API:

  • Gregphoto_Service_Akismet::validateKey – checks whether or not your api key is valid
  • Gregphoto_Service_Akismet::submitSpam – submits a message to Akismet telling it that it is spam (normally a message that Akismet identified as not being spam)
  • Gregphoto_Service_Akismet::submitHam – tell Akismet that a message it identified as spam is not actually spam

To get an API key for yourself, follow the instructions printed on blog.phpdeveloper.org:

You’ll need to go signup over on WordPress.com and, once logged in, go to your “My Dashboard” (top left link), go to the “Users” tab at the top and right there at the top of the page you’ll see “Your WordPress.com API key is…”. Easy as pie.

Signup for a free blog, but don’t worry…you only need to sign up in order to get a key – you don’t actually have to use the wordpress.com account for anything else…

For anyone looking to build a site or application using the Zend Framework that accepts comments – this could be a good little tool that should integrate very cleanly.

Enjoy, and let me know what you think.