Using Akismet in PHP4 to Defeat Comment Spam

March 4, 2006

I've been having comment spam problems on my personal page. Wordpress uses Akismet for comment spam filtering now-a-days, and there are bindings for several languages, including PHP5, Python, and Ruby. The Akismet API documentation documentation has a PHP4-friendly code snippet, but a quick Google search didn't turn up any full-blown PHP4 bindings, so I wrote my own. Here's an example of the API:

#
# Check comment using Akismet (http://akismet.com/).  Returns true for
# spam, and false for ham.
#
function is_comment_spam($news_id, $name, $email, $url, $comment) {
  global $AKISMET_CONFIG;

  # populate comment information
  $comment_data = array(
    'user_ip'               => $_SERVER['REMOTE_ADDR'],
    'user_agent'            => $_SERVER['HTTP_USER_AGENT'],
    'referrer'              => $_REQUEST['REFERER'],
    'permalink'             => "http://paulduncan.org/?id=$news_id",
    'comment_type'          => 'comment',
    'comment_author'        => $name,
    'comment_author_email'  => $email,
    'comment_author_url'    => $url,
    'comment_content'       => $comment,
  );

  # create akismet handle
  $ak = new Akismet($AKISMET_CONFIG['api_key'], 
                    $AKISMET_CONFIG['blog']);

  # return akismet result (true for spam, false for ham)
  return $ak->check_comment($comment_data);
}

Download php4-akismet-0.1.tar.gz (Signature)

Update: Fixed a minor typo in the example.