Aching Brain Just another WordPress weblog

PHP5 Akismet

Introduction

This is a simple little PHP5 class that enables you use the Akismet anti-spam service in your PHP5 application.

Download

Both versions are the same and require PHP5.

Installation

Untar or unzip the archive and put the Akismet.class.php file somewhere accessible to your scripts. Use include or a derivative to import it into your script.

Usage

Before you can use Akismet, you need a WordPress API key (they are free and getting one takes about five minutes). Once you have one, take a look at the code below:

  1. $WordPressAPIKey = 'aoeu1aoue';
  2. $MyBlogURL = 'http://www.example.com/blog/';
  3.  
  4. $akismet = new Akismet($MyBlogURL ,$WordPressAPIKey);
  5. $akismet->setCommentAuthor($name);
  6. $akismet->setCommentAuthorEmail($email);
  7. $akismet->setCommentAuthorURL($url);
  8. $akismet->setCommentContent($comment);
  9. $akismet->setPermalink('http://www.example.com/blog/alex/someurl/');
  10.  
  11. if($akismet->isCommentSpam())
  12.   // store the comment but mark it as spam (in case of a mis-diagnosis)
  13. else
  14.   // store the comment normally

That's just about it. In the event that the filter wrongly tags messages, you can at a later date create a new object and populate it from your database, overriding fields where necessary and then use the following two methods to train it:

  1. $akismet->submitSpam();

and

  1. $akismet->submitHam();

to submit mis-diagnosed spam and ham, which improves the system for everybody. See the included documentation for a complete run-down of all available methods.

Changelog

Version 0.4

  • Performance - changed HTTP version from 1.1 to 1.0 (with thanks to Jan De Poorter).
  • Performance - No longer issues a separate HTTP request to check validity of the API key with every instantiation.
  • Added a new public method 'isKeyValid' to manually check validity of the API key passed to the constructor.
  • The method 'isCommentSpam' (rather than the constructor) will now throw an exception if the API key is invalid.
  • Tidied up internal structure a bit.

Version 0.3

Internal testing version

Version 0.2

Initial release

Version 0.1

Internal testing version

Popularity: 86% [?]

Comments (17) Trackbacks (14)
  1. Work grate ! Thank YOU !

  2. Thanks for the code. It’ll make my life easier.

  3. Thanks for this nice little class. I just came across it in my search for something to battle contact-form spam :)

    One note, as I was looking for a TypePad-class, I ran across a minor style-issue: line 181

    $this->wordPressAPIKey . ‘.rest.akismet.com’

    should be:

    $this->wordPressAPIKey . ‘.’ . $this->akismetServer

  4. This is really great thank you.

  5. Is there any way at all of incorporating this into a phpBB forum? I get loads of spammers trying to register and leave spam posts – Akismet stops them DEAD on my blog!

  6. Is there any way of incorporating this script into a phpBB forum running on MySQL and PHP5? I love Akismet on my blog and would love to have it on my forum as well if possible!

  7. Seems to no longer work, perhaps Akismet changed their API.

  8. Thanks for this script, but it looks like it no longer works.
    I will check to be sure Akismat have not changed their API.

    Thanks again.

  9. You can use http 1.1 if you add “Connection: close” in the http request header.

  10. Same problem here …
    Can`t find any API changes on the akismet page, so I can`t fix it myself … Any ideas?

  11. I use your module on two web sites that I maintain and using your module has helped reduce the amount of spam comments that get posted to the web site by a very large amount.

    I just thought that I’d stop by your web site and say thanks for all your work on this very helpful module.

  12. Found the problem, documented it on my blog here: http://www.chuggnutt.com/2010/02/11/akismet-changed-their-return-values.php. Hope this helps!

  13. Just installed this on my (dutch) website :) great job. I hope it works :P

    (only problem is the guestbook actually xD)

  14. Awesome :) Many thanks!

  15. Great in just used in my guestbook web. That is actualy works! thanks

  16. Brilliant! I am going to use it to make a CakePHP Component


Leave a comment


Pages

Categories

Blogroll

Archive

Meta